Steffen
Steffen

Reputation: 733

How to import a module for all functions?

I have a main file, called 'main.py' and two subfolders. One is called 'externals' and contains 'easygui.py' and the other one is called 'modules' and contains a file called 'gui.py'.

The program is started via main.py

I import easygui in main.py with import externals.easygui. The same way I import modules.gui

But when I call the welcome-function, It does not know easygui, I have to import it in the function. As I want to call more easygui-functions, I don't want to import easygui in every def.

How can this be solved?

Thanks in advance!

Steffen

Examples (for readability without try/except and comment stuff):

main.py:

#!/usr/bin/env python

import externals.easygui
import modules.gui

main():
    gui.welcome()

gui.py:

def welcome():
    msgbox("Welcome!", ok_button="Ok")

Upvotes: 1

Views: 4228

Answers (3)

PythonProgrammi
PythonProgrammi

Reputation: 23443

What we have here...

Import the gui.py module and give it the name gui, so that you just have to use gui label to access to the functions in gui.

In the gui.py module import only the function msgbox, so that you don't have no other stuff imported (as you wanted this).

Now, when the program runs it shows the message calling the gui.welcome function.

main.py

import modules.gui as gui

def main():
    gui.welcome()

if __name__ == "__main__":
    main()

gui.py in subfolder modules

from externals.easygui import msgbox

def welcome():
    msgbox("Welcome!", ok_button = "Ok")

Upvotes: 0

mgilson
mgilson

Reputation: 309841

Assuming that welcome is in easygui.py, you want:

def main():
   externals.easygui.welcome()

As these things can get tedious to type, it's often customary to import subpackages under an abbreviated name:

import externals.easygui as eg
def main():
    eg.welcome()

Alternatively, if you can make the whole thing a package by adding __init__.py, and then you can control the namespace which gets imported from there ...


As far as sideways imports go, here's a test directory structure I set up:

steffen
  |- __init__.py
  |- main.py
  |- easygui
     |- __init__.py
     |- gui.py
  |- external
     |- __init__.py
     |- welcome.py

Now, (for simplicity) each __init__.py simply imports the files/modules contained in that directory. So, in steffen:

#steffen.__init__.py
import main
import easygui
import external

and in external

#steffen/external/__init__.py
import welcome

and so forth.

for the actual code:

main.py:

import easygui
def main():
    easygui.gui.welcome()

easygui/gui.py:

import steffen.external as se
def welcome():
    se.welcome.hello()

external/welcome.py

def hello():
    print "Hello"

Now I can use all of this. In the parent directory of steffen (just to make sure the package steffen is on PYTHONPATH), I can:

import steffen
steffen.main.main()

Phew! Now, it's a little silly to have steffen.main.main(). If you want to refer to the function as just steffen.main(), you can set that up in steffen.__init__.py. Just change it to:

#steffen.__init__.py
from main import main
import easygui
import external

So, if you would call a function by foo.func() in __init__.py, you'll call it as steffen.foo.func() in a script that imports steffen. Likewise, if you would call the function as foo() in __init__.py, you'll call it as steffen.foo() in a script that imports steffen. Hopefully that makes sense. There's a lot to digest in this simplest working example I could come up with. The upside, if you can work through all of this and understand it, then you know almost everything there is to know about writing python packages (we haven't talked about relative imports which could be used here too, or about writing a setup.py to actually install your package, but those are fairly easy to understand once you understand this stuff).

Upvotes: 3

Dunes
Dunes

Reputation: 40683

Each source file needs to have its own imports, as they do not know about the imports of other modules. You must include an import externals.easygui in modules.gui.py at the top of the file.

eg. gui.py

from externals.easygui import msgbox

def welcome():
    msgbox("Welcome!", ok_button="Ok")

Upvotes: 0

Related Questions