Creation of a module

Asked

Viewed 209 times

-8

I’m having a hard time creating my own module. I would like an example with a step by step steps to create a module so I can identify where I am missing.

  • What difficulties? Everything you need to know to start documentation, then I recommend you start reading it and, if you still have questions, come back here and elaborate more your question. Possibly the [Ask] guide will help you better understand how to format the question.

  • I edited the question.

  • You need to elaborate to ask. Elaborate means describe the scenario you have with details and put the point where there is doubt followed by attempts you have already made.

  • Are you trying to run your script? That’s what I understood and in case you can run it by terminal (being in the script dossier): python3 script.py OR python2 script.py

  • Complementing @Anderson with this link . Need to elaborate a little more your question, put the code you are running.

  • 1

    If the difficulty is using Python Interpreter in the documentation also has information about that

  • I edited the question again.

Show 2 more comments

2 answers

1

Whereas you want to put your routines inside a separate file. For example you have the routine media() in your program:

def media(a,b):
    return (a+b)/2.0

print(media(4,2))

Then you put in a separate file, the "modulos.py" and edit your main program to:

from modulos import media

print(media(4,2))

Wishing to add more functions to "modulos.py", just do it:

def media(a,b):
    return (a+b)/2.0

def maior(a,b):
    return a if a>b else b

And then edit your program to load the functions you need:

from modulos import media, maior

Or else load all available functions within it:

from modulos import *

Of course, this is a simple example of modules with all files being in the same directory.

1


You seem to be confused regarding the use of direct import in the interpreter vs the execution of a file .py with Miles inside it. Without the code it is difficult to understand where you are going wrong, but I will start from the point that read the documentation of python modules and did not understand.

In the tutorial of the site he is directly using the interpreter. There, everything it imports will be available to be used in the next commands of the interpreter and only of it.

However, I understand that you want to import and run in a separate file. Using the example of own documentation, you can check here what a python application would look like using a module.

Explaining the code, the file fibo.py provides the functions for calculating the sequence of Fibonacci that the file main.py (shown below) will use:

# Fibonacci numbers module

def fib2(n):   # return Fibonacci series up to n
    result = []
    a, b = 0, 1
    while b < n:
        result.append(b)
        a, b = b, a+b
    return result

Filing cabinet main.py will be used as the main file, to start your python application, importing the module you need:

from fibo import fib2

print(fib2(100))

See that file name (fibo) is the name of the module itself.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.