Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IPython fails to load a module where the standard interpreter works

Tags:

python

ipython

I can't load a python module in IPython that works fine in the normal interpreter. I have analyzed the problem and somehow IPython does not find a module, whereas the standard console does:

This works in the normal interpreter:

>>> import sys
>>> sys.path.append(r'c:\development\...\ns.package-10.1.0.3-py2.7.egg')
>>> from ns import package
>>>

But on IPython it does not:

In [2]: import sys

In [3]: sys.path.append(r'c:\development\...\ns.package-10.1.0.3-py2.7.egg')

In [4]: from ns import package
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-4-c019e2988e33> in <module>()
----> 1 from ns import package

ImportError: cannot import name package

I find this pretty confusing. I am new to IPython and I don't know where to start. Thanks in advance.

like image 799
bgusach Avatar asked Apr 22 '15 10:04

bgusach


People also ask

Is IPython an interpreter?

IPython (short for Interactive Python) was started in 2001 by Fernando Perez as an enhanced Python interpreter, and has since grown into a project aiming to provide, in Perez's words, "Tools for the entire life cycle of research computing." If Python is the engine of our data science task, you might think of IPython as ...

How do you resolve a module not found error in Jupyter notebook?

Finally, if you are getting this the ModuleNotFoundError in a Jupyter notebook, then you need to ensure that both Jupyter and scikit-learn are installed within the same environment. As mentioned already, it is much better (and will definitely save you time and energy) if you work in isolated environments.

How do I import a module into Jupyter lab?

Create a folder called startup if it's not already there. Add a new Python file called start.py. Put your favorite imports in this file. Launch IPython or a Jupyter Notebook and your favorite libraries will be automatically loaded every time!


1 Answers

The key thing you have to recall here is that usually there's not just one python interpreter on your machine. Many systems nowadays come with both python2.7 and python3.x, maybe there are more. Every interpreter maintains its own set of installed packages and has its own set of installed scripts, such as ipython or pip. When you type pip in your shell it's often not obvious which pip you are actually calling. Is it python3's or python2's pip?

And here is where you can get into trouble:

The ipython und python executables in your PATH do not necessary belong to the same interpreter: Imagine that python and pip belong to a python2 installation but you then decide to install ipython into your python3 interpreter. Now ipython sees the the packages of your python3 interpreter whereas python sees all your python2 packages.

If you compare the output of which ipython and which python in this case, you will notice that you get paths that belong to different interpreters.

So how can you call the script for your favorite interpreter? If python points to your favorite interpreter some packages give you a nice way of calling via -m parameter: Instead of pip install ipython you can write python -m pip install ipython and be sure that you called the pip version of your favorite python interpreter.

Similar you can start ipython notebook via python -m IPython notebook.

like image 192
cel Avatar answered Oct 23 '22 10:10

cel