How to run ipython from venv?

2.1k Views Asked by At

My ipython works okay, but when I try to open ipython console from venv I am getting:

Traceback (most recent call last):
  File "/usr/bin/ipython3", line 4, in <module>
    from IPython import start_ipython
ImportError: No module named 'IPython'

Any ways to do this?

3

There are 3 best solutions below

8
On

have you activated your virtualenv and installed ipython into the virtualenv?

source path/to/venv/bin/activate
pip install ipython
2
On

Did you by any chance create the venv with the --system-site-packages flag and install ipython with pip? This combination is broken in my experience (Python 3.6.2, pip 9.0.1).

Two workarounds are:

  1. Use virtualenv instead of venv,
  2. Or use easy_install instead of pip.

With pip, scripts get the wrong shebang pointing to system python.

0
On

It seems that now it works. You should be able to achieve that:

user@host:~/ source path/to/venv/bin/activate
(venv) user@host:~/ python3 -m pip install ipython
(venv) user@host:~/ ipython
Python 3.8.10 (default, Nov 26 2021, 20:14:08)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.31.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: import sys

In [2]: sys.executable
Out[2]: '/path/to/env/bin/python3'

In [3]:

Note that I am using python3 -m pip [...] to ensure that the module are installed in the environment and not elsewhere.