I launch IPython from the main folder /project
. Now if I make changes in the file /project/tests/some_module.py
, the changes fail to be autoreloaded in IPython. Also, I get the following message after I save the changes and want to run some other script in the prompt:
[autoreload of some_module failed: Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/IPython/extensions/autoreload.py", line 229, in check
superreload(m, reload, self.old_objects)
ImportError: No module named some_module]
It seems it detects changes were made inside folder /tests
but it cannot import it. Can someone help me with this?
Edit:
For better clarification: I launch IPython from the terminal in the main folder. In this folder, I have another folder tests
. Inside tests
I have two python files:
some_module.py:
def hello_world():
print "Hello World!!!"
use_some_module.py:
from some_module import hello_world
hello_world()
Once I launched IPython, further changes in some_module.py won't be loaded in IPython. For example, if I add a second print "Hello Earth!!!"
in the definition of hello_world()
, and run run tests/use_some_module.py
, I get the error message shown above, and will only get the "Hello World!!!"
print.
Edit2: I would like a solution where I don't need to either change the working directory, or adding any search paths manually. I want it to be loaded automatically with autoreload.
If all you need is to reload a changed file in Python just do the following:
But if your reload purposes are really eager, you can do the following (taken from this question):
The previous code will reload all changed modules every time before executing a new line.
NOTE: You can also check dreload which does a recursive reload of a module, and %run which allows you to run any python script and load all of its data directly into the interactive namespace.
Hope it helps,
Try changing your file
use_some_module.py
to:Reload with
from import
usually does not work.This works for me:
Module
/project/tests/some_module.py
must be importable from/project
. It means/project/tests
must be insys.path
.Either change to
/project/tests
and%run use_some_module.py
or dosys.path.insert(0,tests)
to inserttests
into module search path.If
some_module
is not in search path, IPython'sautoreload
never gonna find it.Another way to make it work is to make the
tests
a package by creating__init__.py
in it. And use relative import inuse_some_module
asfrom .some_module import hello_world
. Now do these from IPython prompt,That's you have to run
use_some_module
module in thetests
package as a script.The first time you do
%run tests/use_some_module.py
, which is run as if you were runningpython tests/use_some_module.py
. So/project/tests
which the script is in, is automatically included insys.path
. That's whyfrom some_module import hello_world
inuse_some_module
succeeds. Additionally, after the run, objects inuse_some_module
's global namespace are available in IPython session.But when you change
tests/some_module
, it has to be loaded again to see the changes. Toreload
it manually, it has to be imported first. Nowimport
should succeed becauseuse_some_module
imported it first when it was run,some_module
is insys.modules
. Butreload
to succeed,some_moudle
has to be in search path. So, even manualreload
would fail let aloneautoreload
.Another solution is to start IPython as
PYTHONPATH=tests ipython
to includetests
directory insys.path
.Or set this
c.InteractiveShellApp.exec_lines = ['import sys','sys.path.insert(0,"tests")']
inipython_config.py
..