EDIT: is from __future__ import absolute_import
available in IronPython? I just realized, though everything I'm using is only python, the project as a whole is in IronPython.
I'm trying to use relative imports (instead of appending to sys.path). Here is my directory structure:
-src
|
-runners
|
-__init__.py
-clippyRunner.py
|
-__init__.py
-clippy.py
-irondb.py
-ironxl.py
now, in clippyRunner.py, I want to import clippy, which as you can see is in the paren开发者_如何学Pythont directory src
. so what I've done is this:
from __future__ import absolute_import
from ...src import clippy
but this is giving me this error:
ValueError: Attempted relative import in non-package
i've also tried
from ..src import clippy
witht he same results.
what am I doing wrong here?
EDIT: I've also tried import ..clippy
which just gives me: SyntaxError: unexpected token '.'
The error message you're getting says that IronPython is accepting the syntax, but believes you aren't currently running code inside a package.
Since you're getting '__main__'
instead of 'src.runners.clippyRunner'
when you print out __name__
, this tells me that you're running /src/runners/clippyRunner.py
directly from the command line. As you've seen, this breaks relative imports, since Python doesn't know where the module lives in the package heirarchy.
In CPython, you can handle this situation by using the command line python -m src.runners.clippyRunner
from the directory containing the src
package to ensure the interpreter knows the proper location of the affected module.
I don't know if the current version of IronPython has an equivalent mechanism (CPython only fixed it when PEP 366 was included in version 2.6)
Your import is broken:
from .. import clippy
精彩评论