14

Basically I'm asking the same question as this guy: How to do relative imports in Python?

But no one gave him a correct answer. Given that you are inside a subfolder and you want to go up a directory and then into ANOTHER subfolder, doing what they suggested does not work (as the OP pointed out in his comments to their answers).

I know that you can do this by using sys.path, but I would prefer a cleaner method.

Example:

App
__init__.py
Package_A
--__init__.py
--Module_A.py
Package_B
--__init__.py
--Module_B.py

How would I import Module_A into Module_B?

Community
  • 1
  • 1
ryeguy
  • 65,519
  • 58
  • 198
  • 260
  • By "Module_A" do you mean "Package_A"? A package contains an "__init__.py and, possibly, additional Modules. Can you rewrite your "Module_A"'s and "Module_B"'s to be "Package_A" and "Package_B" so your question is more clear? – S.Lott Jan 21 '09 at 00:28

3 Answers3

12
main.py
setup.py
app/ ->
    __init__.py
    package_a/ ->
       __init__.py
       module_a.py
    package_b/ ->
       __init__.py
       module_b.py
  1. You run python main.py.
  2. main.py does: import app.package_a.module_a
  3. module_a.py does import app.package_b.module_b

Alternatively 2 or 3 could use: from app.package_a import module_a

That will work as long as you have app in your PYTHONPATH. main.py could be anywhere then.

So you write a setup.py to copy (install) the whole app package and subpackages to the target system's python folders, and main.py to target system's script folders.

nosklo
  • 217,122
  • 57
  • 293
  • 297
  • The main thing is to add the appropriate `__init__.py` files to each directory and to include `app` in your import statement. If you do that, adding the line `import app.package_a.module_a` to `module_b.py` should do the appropriate import. Basically, step 3 is what you want, the rest is extraneous unless you're planning on distributing the package. – Nick Crawford Oct 24 '11 at 14:52
2

If I'm reading correctly, in Python 2.5 or higher:

from ..Module_B import Module_B

I thought I was well-versed in Python but I had no idea that was possible in version 2.5.

David Z
  • 128,184
  • 27
  • 255
  • 279
  • That's correct. It's a bit ugly though, and absolute imports are generally the best thing except in special circumstances. – bobince Jan 21 '09 at 00:38
  • 2
    THis doesn't work I get "attempted relative import in non-package" – ryeguy Jan 21 '09 at 01:01
  • The statement is assumed to be inside Module_A.py (and, post-edit, should be from ..Package_B.) relative imports are based on the package path of the module you are inside, not directories; you can't use them from a top-level script or simple module. – bobince Jan 21 '09 at 02:20
0

If you are then importing Module_B in to App, you would

Module_B.py: import ModuleA

App.py (which also imports ModuleA which is now by default in your Pythonpath)

import Module_B.Module_B

Another alternative, is to update __init__.py (the one in Module_A/App folder) to:

import os
import sys
sys.path.extend('%s../' % os.getcwd())
import ModuleA

Another alternative, is to add your folder to the PYTHONPATH environment var.

Nick Stinemates
  • 41,511
  • 21
  • 59
  • 60
  • Modifying `sys.path` at runtime is nearly always wrong. It's fragile and indicates a badly-structured package. – habnabit Jan 03 '10 at 14:12