rodgdor
rodgdor

Reputation: 2640

Module not found, but only in ipython3

I have written a package named biographs with the following architecture:

biographs (folder)
 >biographs (package)
  >__init__.py
  >bpdb.py
  >pmolecule.py
  >bgraph.py
  >bspace.py

The __init__.py file is only the following:

from .pmolecule import Pmolecule

When I'm working in ipython3 and I want to import biographs (only to use the class Pmolecule), I get the following error in ipython3 (Ipython 6.0.0, Python 3.6.1):

In [1]: cd ~/biographs/
/Users/rdora/biographs

In [2]: import biographs
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-2-1803e6928e0e> in <module>()
----> 1 import biographs

/Users/rdora/biographs/biographs/__init__.py in <module>()
----> 1 from .pmolecule import Pmolecule

/Users/rdora/biographs/biographs/pmolecule.py in <module>()
      1 # class to deal with protein structures
      2 # python 2
----> 3 import bpdb
      4 import bgraph
      5 import bspace

ModuleNotFoundError: No module named 'bpdb'

However when I do exactly the same process using IPython 5.3.0 with Python 2.7.13 there is no error message.

Thank you

Upvotes: 0

Views: 340

Answers (1)

a_guest
a_guest

Reputation: 36329

This is because of how imports work in Python 2 and Python 3. In your module pmolecule.py you apparently do import bpdb. In Python 2 this will search the local directory for a module called bpdb.py and import it. However in Python 3 you must be explicit about those relative imports, i.e. you need to do

from . import bpdb

In order to get consistency for Python 2 you can use from __future__ import absolute_imports which prohibits such non-explicit imports also under Python 2.

Note that the same holds for:

----> 3 import bpdb
      4 import bgraph
      5 import bspace

Those need to be imported via from . import <module-name> syntax.


Further reading

Upvotes: 2

Related Questions