Installing ipython inside virtual environment

7.5k Views Asked by At

I have installed python 3.3.6 and ipython 3.0.0 from source on my account. I am on a Linux cluster (RedHad4.9). When I start ipython I get the following message:

WARNING: Attempting to work in a virtualenv. If you encounter problems, please install IPython inside the virtualenv.
WARNING: IPython History requires SQLite, your history will not be saved.

I am not sure what a virtualenv is, and why I am attempting to work in a virtualenv. I have been looking into this for a while but I cannot find a solution. Could anybody hint me the right way?

3

There are 3 best solutions below

3
On

Since you installed python and iPython from source, this seems to be the best fix: A simple way to fix this would be to install Python in a non-root location, and use pip to install iPython. It's finding iPython in the global modules by not in the local modules, so it assumes you're using a virtualenv. Just use a pip install.

As for virtual environments, they're a virtual python install that restricts access to global modules so you can test custom modules, do other cool stuff. For example, you can activate and deactivate different environments. After installing one with (to the same directory):

$ virtualenv .
$ source bin/activate

Then I'm running in a virtualenv. Since iPython the command is found, but the global Python differs from the one installed, it gives me a warning though. https://virtualenv.pypa.io/en/latest/

Normally, if you're running a virtual environment, your shell should reflect this:

Without virtualenv:

alex@alex-Gazelle-Professional:~$

With VirtualEnv:

(no-pyside)alex@alex-Gazelle-Professional:~$

EDIT:

Difference between local and global modules. The path with which Python searches for installed modules can be as follows:

import sys; print sys.path ['', '/home/alexvirtualenvs/no-pandas/lib/python2.7', '/home/alexvirtualenvs/no-pandas/lib/python2.7/plat-x86_64-linux-gnu', '/home/alexvirtualenvs/no-pandas/lib/python2.7/lib-tk', '/home/alexvirtualenvs/no-pandas/lib/python2.7/lib-old', '/home/alexvirtualenvs/no-pandas/lib/python2.7/lib-dynload', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/home/alexvirtualenvs/no-pandas/local/lib/python2.7/site-packages', '/home/alexvirtualenvs/no-pandas/lib/python2.7/site-packages']

This is in a virtual environment. See how the search path does not include /usr/lib, etc. If I deactivate my virtualenv and search globally, I get the following:

>>> import sys; print sys.path
['', '/usr/local/lib/python2.7/dist-packages/h5py-2.5.0-py2.7-linux-x86_64.egg', '/usr/local/lib/python2.7/dist-packages/XlsxWriter-0.7.3-py2.7.egg', '/usr/local/lib/python2.7/dist-packages/pyqtgraph-0.9.10-py2.7.egg', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.7', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client', '/usr/lib/python2.7/dist-packages/wx-3.0-gtk2']

In here, everything besides the '' (current directory) is installed globally.

0
On

I have encountered the same error and I got answer and enough explanation on github.

As per the edit suggested, try and execute following steps:

import os, sys, IPython
print os.environ['VIRTUAL_ENV']
print sys.executable
print IPython.__file__
print sys.path

This will print the paths of virtualenv, ipython executable. And after doing this hit head $(which ipython) and hash -r. Now try and open ipython in new tab of the terminal. The error go away. The reasons are:

  1. The path of commands is cached after you called them, so it doesn't actually search your PATH for a given command more than once in a shell session.
  2. which is not aware of this cache, so which ipython does not necessarily point to the ipython that will be called if it has been called before in the shell session.
  3. hash -r simply resets this cache, so that which will be accurate once again.
0
On

Have you set VIRTUAL_ENV, echo $VIRTUAL_ENV to check your VIRTUAL_ENV, or export VIRTUAL_ENV=/path/to/virtual_env in your .bashrc, and then open a new terminal!