user2881553
user2881553

Reputation:

IPython: ImportError: No module named mymodule

I am experience this annoying error message, every time after I have updated my module and try to reload it.

I do have a module mymodule in a package mypackage that has a __init___.py file in it.

When I do

from mypackage import mymodule

everything is ok.

After I update the module and reload it with

reload(mymodule)

Error pops up:

In [4]: 
   ...: reload(constants)
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-4-264a569b44f9> in <module>()
      1 
----> 2 reload(mymodule)

ImportError: No module named mymodule

To resolve this, I have to kill my interpreter and re-import everything when I want to reload one module, which is extremely time-consuming and annoying. How may I fix it?

PS: I suspect this is something wrong with PYTHONPATH, but since I am using Python tool for Visual Studio, I cannot find the PYTHONPATH option.


Update

As far as I remember, it seems that things start going wrong immediately after I have this

import os
os.chdir(constants.PROJECT_PATH + '//data//')

in one of the modules. Yet does it really matter?

I don't think it matters, as the path in the brackets is exactly my project path.

Upvotes: 2

Views: 9985

Answers (2)

Michael Kazarian
Michael Kazarian

Reputation: 4462

Try it:

import os, sys
my_lib_path = os.path.abspath('../../../mypackage')
sys.path.append(my_lib_path)
from mypackage import mymodule

or add your package into PYTHONPATH. For unix it:

$ export PYTHONPATH=/absolute/path/to/mypackage

Upvotes: 3

mpenkov
mpenkov

Reputation: 21922

Is your package in the present working directory?

When the interpreter comes across an import libraryname statement, it looks for libraryname in several locations: the present working directory, directories specified by the PYTHONPATH environment variable, and some installation dependent paths.

So as long as your module is in the present working directory, the interpreter is able to find it. However, once the pwd changes, the interpreter isn't able to find the module anymore, and the import fails. You really have two options:

  1. Install your module in a location where Python can find it. Typically, Python packages are located in /usr/lib on Linux systems (not sure about Windows, but you could easily find out). If you put your package there, then the interpreter will pick it up. I wouldn't recommend doing this by hand; write a simple setup.py script to handle the installation for you.
  2. Tell Python where to explicitly look for the package. This is usually done through the PYTHONPATH environment variable. Set that at the command line before invoking the interpreter (or at the system-level, if you must).

If you can't change PYTHONPATH for some reason, then you can modify the path during runtime:

import sys
sys.path.append(your_directory_here)

This is a pretty ugly way to deal with the problem, so should be a last resort.

Upvotes: 1

Related Questions