Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python relative import example code does not work [duplicate]

Tags:

python

import

Possible Duplicate:
How to properly use relative or absolute imports in Python modules?

I have this file layout, as shown in this example: (download here: http://www.mediafire.com/?oug42nzvxrvoms4) http://www.python.org/dev/peps/pep-0328/#guido-s-decision

moduleX contains:

from .moduleY import spam
from .moduleY import spam as ham
from . import moduleY
from ..subpackage1 import moduleY
from ..subpackage2.moduleZ import eggs
from ..moduleA import foo
from ...package import bar
from ...sys import path

and this is what happens:

C:\package\subpackage1>python moduleX.py
Traceback (most recent call last):
  File "moduleX.py", line 1, in <module>
    from .moduleY import spam
ValueError: Attempted relative import in non-package

I have python 2.7.2. I have

__init__.py

files in every directory. Why does this code not work?

like image 528
Josiah Avatar asked Feb 03 '12 02:02

Josiah


People also ask

How do you use relative imports in Python?

Relative imports use dot(.) notation to specify a location. A single dot specifies that the module is in the current directory, two dots indicate that the module is in its parent directory of the current location and three dots indicate that it is in the grandparent directory and so on.

Does import copy code Python?

No. Importing is not #include . It doesn't dump another file into yours.

Why do I need __ init __ PY?

The __init__.py files are required to make Python treat directories containing the file as packages. This prevents directories with a common name, such as string , unintentionally hiding valid modules that occur later on the module search path.

Should I use relative or absolute imports Python?

With your new skills, you can confidently import packages and modules from the Python standard library, third party packages, and your own local packages. Remember that you should generally opt for absolute imports over relative ones, unless the path is complex and would make the statement too long.


1 Answers

From the docs:

  • http://www.python.org/dev/peps/pep-0328/#guido-s-decision

you can see this:

Relative imports use a module's name attribute to determine that module's position in the package hierarchy. If the module's name does not contain any package information (e.g. it is set to 'main') then relative imports are resolved as if the module were a top level module, regardless of where the module is actually located on the file system.

By running it as python moduleX.py, you are doing exactly the above. Instead, try this:

python -m package.subpackage1.moduleX

This will import moduleX and put the top level at package. Run from the top of the hierarchy:

package/
    __init__.py
    subpackage1/
        __init__.py
        moduleX.py
        moduleY.py
    subpackage2/
        __init__.py
        moduleZ.py
    moduleA.py

i.e. in your case from c:\ directly:

c:\>python -m package.subpackage1.moduleX

Note one thing - the imports in moduleX.py are these:

from .moduleY import spam
from .moduleY import spam as ham
from . import moduleY
from ..subpackage1 import moduleY
from ..subpackage2.moduleZ import eggs
from ..moduleA import foo
from ...package import bar
from ...sys import path

The second to last:

from ...package import bar

requires the root folder (c:\ in your case) to be a package (i.e. have __init__.py). Also, it requires bar variable defined in package\__init__.py, which is currently not there (so put bar = 'bar!' there for test). It also requires you to be one level up - so you have to put the package folder in another folder (so you end up with c:\toppackage\package) and run c:\python -m toppackage.package.subpackage1.moduleX.

For this line:

from ...sys import path

there's a note in the above PEP 328 link:

Note that while that last case is legal, it is certainly discouraged ("insane" was the word Guido used).

See also other SOqs about this that might help:

  • How to do relative imports in Python?
  • Can anyone explain python's relative imports?
  • How to accomplish relative import in python
  • python relative import weirdness
  • Relative imports in Python

Hope this helps.

like image 136
icyrock.com Avatar answered Oct 12 '22 12:10

icyrock.com