-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Absolute/relative import not working? #43535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Trying to import from a module using dotted import syntax produces ValueError: Relative importpath too deep This behavior has been confirmed on Mac OS X 10.4 using the Python The exception is raised regardless of whether the PYTHONPATH from __future__ import absolute_import To test, I tried to re-create the package structure used as an example Most of the files were empty, except moduleX.py and moduleY.py: #moduleX.py:
from __future__ import absolute_import
from .moduleY import spam
#moduleY.py:
spam = "spam" According to the PEP, if should be possible to import moduleX without $ python2.5 moduleX.py
Traceback (most recent call last):
File "moduleX.py", line 3, in <module>
from .moduleY import spam
ValueError: Relative importpath too deep Is this a usage/documentation error? |
Logged In: YES Seeing the same behaviour; OSX with the installer. |
Logged In: YES I think this is a usage error. The problem is that If you want to test relative import, try opening an >>> from package.subpackage1 import moduleX
>>> moduleX.spam
'spam' |
Logged In: YES Hm... but the same error occurs if one tries to import moduleX from an In other words, in 2.5b1 any module which uses relative imports can be used $ python2.5 -m package.subpackage1.moduleX
...
from .moduleY import spam
ValueError: Relative importpath too deep Given other efforts (PEP-299; PEP-338) to make it easier to use modules both |
Logged In: YES See the discussion started at: It's not a bug in 328 or 338 (the PEP that adds the -m |
Logged In: YES http://mail.python.org/pipermail/python-dev/2006-June/066197.html Nick you made mention of changing the docs, but I'm not sure |
Logged In: YES The issue isn't actually unique to the -m switch - the With the old implicit relative imports this behaviour is You should be able get something similar to the old implicit if __name__ = '__main__':
from os.path import dirname
__path__ = [dirname(__file__)]
del dirname This makes the relative import machinery treat your main When I went to document this, I got stuck on the fact that However, I'll add a section to PEP-328 about '__main__' and |
Logged In: YES All that said, PEP-328 currently says that "from ...sys I tried it, and it currently fails - the ValueError gets I believe fixing this would also fix Mitch's problem - an I also have an idea regarding the -m switch that I will |
Logged In: YES Patch attached that allows relative imports from a main So to get the PEP-328 example to work, you'd have to run it as: $python2.5 -m package.subpackage1.moduleX The patch relies on a feature added to runpy in rev 47142 Patch attached directly to the bug report because it's |
Logged In: YES Punting on this until 2.6. See updated PEP-338 for the gory |
PEP-366 has been implemented for 2.6, which fixes this bug. The fix |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: