Take the 2-minute tour ×

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!


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


#!/usr/bin/env python

import externals.easygui
import modules.gui



def welcome():
    msgbox("Welcome!", ok_button="Ok")
share|improve this question
It's bit confusing where all your functions are coming from (where is msgbox meant to be coming from). And you talk of welcome being in gui.py and then say it's in a file called welcome.py. I think you need to re-read your question and edit it appropriately. –  Dunes Oct 10 '12 at 12:32
True. In the code examples was an naming error. It is gui.py. welcome() is in gui.py. Fixed. –  Steffen Oct 10 '12 at 12:36
add comment

2 Answers

up vote 3 down vote accepted

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

def main():

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():

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:

  |- __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:

import main
import easygui
import external

and in external

import welcome

and so forth.

for the actual code:


import easygui
def main():


import steffen.external as se
def welcome():


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

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:

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).

share|improve this answer
welcome() is in gui, not in easygui. There are two different folders with two different py-files. –  Steffen Oct 10 '12 at 12:28
@Steffen -- So you're saying that files in the modules directory depend on files in the externals directory? –  mgilson Oct 10 '12 at 12:31
Yes. Is this strange ? :) –  Steffen Oct 10 '12 at 12:37
@Steffen -- Not necessarily -- but you'll need to make them packages. Let me try to hack something together to demonstrate –  mgilson Oct 10 '12 at 12:39
@Steffen -- Demonstration finished. –  mgilson Oct 10 '12 at 13:01
show 4 more comments

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():
share|improve this answer
Ok, that's the way I'm doing it right now. I thought there might be a way to do a global import. –  Steffen Oct 10 '12 at 12:38
I know of no programming language that allows global imports that apply to all modules/classes. Nor do I think there should be. What you suggest would mean that gui.py doesn't even know its own dependencies. –  Dunes Oct 10 '12 at 13:03
Nope. I thought of a way of importing described above by @mgilson –  Steffen Oct 10 '12 at 13:19
I don't really see how that answers your question. gui.py still knows it's dependencies. It depends on init.py which depends on the relevant module. –  Dunes Oct 10 '12 at 13:52
add comment

Your Answer


