Issue
I am working with a Python package that uses relative imports. So from one module, local imports look like:
from . import module
The package is structured like so:
package/
__init__.py
module1.py
module2.py
moduleN.py
subpackage1/
__init__.py
module1.py
module2.py
subpackage2/
__init__.py
...
etc.
I would like to import one of these modules to try out its functions and classes in isolation.
So, I cd
ed into the package/
directory. Then I tried ipython module2.py
and got:
ValueError: Attempted relative import in non-package
I also tried ipython -m module2
and in addition to ValueError
, I also got:
WARNING: Unknown failure executing module: <module2>
I also tried the same thing from one directory up and got the same errors.
How can I import the modules and use their classes and functions inside of IPython?
Solution
Open ipython
from inside of the top-level directory of the project, in this case, package/
.
Then import the modules with absolute imports:
import package.module2 as module2
or
import package.submodule1.module1 as m1
Answered By - DJG
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.