IPython call from virtual

I understand that IPython is not virtualenv-aware and that the most logical solution for this is to install ipython in each virtual server separately, using

pip install ipython 

So far so good. One thing I noticed is that if a system-wide copy of IPython is called from virtualenv using $> ipython before IPython is installed under that virtualenv, subsequent $> ipython will continue to print a system-wide copy of ipython.

On the other hand, if ipython is not called before installing it under virtualenv $> ipython , it will open a new installed copy.

What is the explanation for this?

It also makes me wonder if this behavior means that I should expect some problems?

+73
python virtualenv ipython
Dec 02 '13 at 11:51
source share
8 answers

alias ipy="python -c 'import IPython; IPython.terminal.ipapp.launch_new_instance()'"

This is a great way to always make sure that the ipython instance always belongs to the python virtualenv version.

This only works on ipython> 2.0.

Source

+94
Oct 21 '14 at 8:34
source share

You can force IPython to use the virtual environment, if available, by adding the file below ~/.ipython/profile_default/startups :

 import os import sys if 'VIRTUAL_ENV' in os.environ: py_version = sys.version_info[:2] # formatted as XY py_infix = os.path.join('lib', ('python%d.%d' % py_version)) virtual_site = os.path.join(os.environ.get('VIRTUAL_ENV'), py_infix, 'site-packages') dist_site = os.path.join('/usr', py_infix, 'dist-packages') # OPTIONAL: exclude debian-based system distributions sites sys.path = filter(lambda p: not p.startswith(dist_site), sys.path) # add virtualenv site sys.path.insert(0, virtual_site) 

I recommend calling it 00-virtualenv.py so that changes are made as early as possible.

Note. Make sure ipython is installed in the new virtual environment to make this work.

+10
Jun 04 '15 at 17:54
source share

The answer given by @SiddharthaRT is good! Following this approach makes it easier for me:

 python -m IPython 

This will use the IPython module through the Python basket, ensuring that it references the basket from the env virtual environment.

+9
Oct 18 '18 at 13:43 on
source share

As already mentioned, the latest versions of ipython support virtualenv, so you can use the Activate virtualenv bin script to start ipython using your virtualenv, for example,

 $ source venv/bin/activate (venv) $ ipython WARNING: Attempting to work in a virtualenv. If you encounter problems, please install IPython inside the virtualenv. 
+4
Jul 20 '16 at 18:58
source share

If you try to open the laptop, even ipython 5 will not help - ipython will ignore virtualenv (at least on my computer / setup). You will need to use the rgtk script, but be sure to change the optional part of the filter and sys.path.insert, as shown below:

 import os import sys if 'VIRTUAL_ENV' in os.environ: py_version = sys.version_info[:2] # formatted as XY py_infix = os.path.join('lib', ('python%d.%d' % py_version)) virtual_site = os.path.join(os.environ.get('VIRTUAL_ENV'), py_infix, 'site-packages') dist_site = os.path.join('/usr', py_infix, 'dist-packages') # OPTIONAL: exclude debian-based system distributions sites # ADD1: sys.path must be a list sys.path = list(filter(lambda p: not p.startswith(dist_site), sys.path)) # add virtualenv site # ADD2: insert(0 is wrong and breaks conformance of sys.path sys.path.insert(1, virtual_site) 
  • ADD1: in the original script, we return the filter object, we will break sys.path, and the insert below will fail
  • ADD2: see this question and python documentation
+1
Jan 02 '17 at 11:25
source share

(Debian / Ubuntu), if some version (x) of Python3 is installed, then:

 $ sudo apt-get install -y ipython $ virtualenv --python=python3.x .venv $ source .venv/bin/activate $ pip3 install ipython $ ipython3 

will start ipython running your version of Python3.

+1
Jun 05 '18 at 20:22
source share
  1. Activate your virtual environment using source ~ / .virtualenvs / my_venv / bin / activ or by running workon my_venv . (Depending on how you installed my_venv virtual environment)

  2. Install ipython

pip install ipython

  1. Now run ipython from my_venv.

If it still loads system ipython, run

hash -r

0
Aug 14 '19 at 8:37
source share

I will call in a few years in the hope that someone will find this useful.

This solution solves several problems:

  • You do not need iPython installed in the current virtualenv, only for global Python corresponding to your version of Python virtualenv ( 3.6 != 3.7 ).
  • Works for pyenv users, where your global Python version may be 3.7 and your local Python virtualenv version 3.6 , so using global ipython will fail.
  • Works outside of virtual environments (although not particularly useful since it is always aimed at python ).

Add this to your ~/.bashrc or ~/.zshrc or whatever you have:

 # This is a roundabout way to start ipython from inside a virtualenv without it being installed # in that virtualenv. The only caveot is that the "global" python must have ipython installed. # What this function does that different than simply calling the global ipython is it ensures to # call the ipython that is installed for the same major.minor python version as in the virtualenv. # This is most useful if you use pyenv for example as global python3 could be 3.7 and local # virtualenv python3 is 3.6. function ipy { local PY_BIN local IPYTHON local PYV # This quick way will work if ipython is in the virtualenv PY_BIN="$(python -c 'import sys; print(sys.executable)')" IPYTHON="$(dirname "$PY_BIN")/ipython" if [[ -x "$IPYTHON" ]]; then "$IPYTHON" else # Ask the current python what version it is PYV="$(python -c 'import sys; print(".".join(str(i) for i in sys.version_info[:2]))')" echo "Looking for iPython for Python $PYV" # In a new shell (where pyenv should load if equipped) try to find that version PY_BIN="$($SHELL -i -c "python$PYV -c 'import sys; print(sys.executable)'")" "$(dirname "$PY_BIN")/ipython" fi } 

Then source or open a new terminal and start ipy .

0
Aug 19 '19 at 17:03
source share



All Articles