8

This is my package hierarchy

app  
|--__init__.py    //Empty file
|--server.py  
|--global_vars.py  
|
|--handlers    
   |--__init__.py    //Empty file
   |
   |--url1
   |  |--__init__.py    //Empty file
   |  |--app1.py
   |  |--app2.py
   |
   |--url2
      |--__init__.py    //Empty file
      |--app3.py

Now I want to import global_vars.py inside app1.py. So I gave import app.global_vars.py inside app1.py.

But I get the following error:

    import app.global_vars
ImportError: No module named app.global_vars

I should also mention that I am importing app1.py from server.py. server.py is the file I am actually running. When server.py imports app1.py, app1.py tries to import global_vars.py and I get the above mentioned error

What am I doing wrong here?

11
  • 2
    Is there a __init__.py file in app?
    – Martijn Pieters
    May 28, 2013 at 7:44
  • Yes, there is a __init__.py in all the directories in the given file heirarchy
    – Hashken
    May 28, 2013 at 7:48
  • What does import app; print app.__file__ say is the path to that module / package?
    – Martijn Pieters
    May 28, 2013 at 7:56
  • I went to app's parent folder, opened a python shell and imported app. Now app.__file__ gives app/__init__.py as output
    – Hashken
    May 28, 2013 at 7:59
  • No, I wanted you to do that in app1.py.
    – Martijn Pieters
    May 28, 2013 at 8:01

2 Answers 2

14

If you are running app/server.py as a script, the parent directory of app is not added to sys.path(). The app directory itself is added instead (not as a package but as a import search path).

You have 4 options:

  1. Move server.py out of the app package (next to it)
  2. Add a new script file next to app that only runs:

    from app import server
    server.main()
    
  3. Use the -m switch option to run a module as the main entry point:

    python -m app.server
    
  4. Add the parent directory of server.py to sys.path:

    import os.path
    import sys
    
    parent = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    sys.path.insert(0, parent)
    

    This last option can introduce more problems however; now both the app package and the modules contained in the app package are on sys.path. You can import both app.server and server and Python will see these as two separate modules, each with their own entry in sys.modules, with separate copies of their globals.

1
  • Thank you for your help. Now, I understand the problem.
    – Hashken
    May 28, 2013 at 8:15
0

need __init__.py file, will regard it as a package

app
|--server.py
|--global_vars.py
|--handlers

|--__init__.py ...

__init__.py can be empty

1
  • There is a __init__.py in all the directories in the given file heirarchy
    – Hashken
    May 28, 2013 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.