DJG
DJG

Reputation: 6563

Playing with packages in IPython

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 cded 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?

Upvotes: 0

Views: 143

Answers (1)

DJG
DJG

Reputation: 6563

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

Upvotes: 1

Related Questions