pyenv-virtualenv icon indicating copy to clipboard operation
pyenv-virtualenv copied to clipboard

Python and Ipython versions differ inside virtualenv

Open CMLL opened this issue 7 years ago • 7 comments

I'm creating a local virtualenv using 2.7.6 which I have setup as the global version, but the Python and IPython interpreter goes back to using the system 2.7.11 as soon as is activated.

➜  ~ pyenv versions
  system
* 2.7.6 (set by /home/cllamach/.pyenv/version)
  2.7.6/envs/controlpanel
  2.7.6/envs/testing_env
  controlpanel
  testing_env

➜  ~ pyenv virtualenv 2.7.6 testing_env

(testing_env) ➜  ~ python
Python 2.7.11+ (default, Apr 17 2016, 14:00:29) 
[GCC 5.3.1 20160413] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

(testing_env) ➜  ~ ipython
Python 2.7.11+ (default, Apr 17 2016, 14:00:29) 
Type "copyright", "credits" or "license" for more information.

IPython 5.0.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: 

I have setup the 2.7.6 as global and shell version and nothing changes. And the virtualenv is being created in the correct version from response of the uninstall.

(testing_env) ➜  ~ pyenv uninstall testing_env
pyenv-virtualenv: remove /home/cllamach/.pyenv/versions/2.7.6/envs/testing_env? 

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/35936439-python-and-ipython-versions-differ-inside-virtualenv?utm_campaign=plugin&utm_content=tracker%2F335155&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F335155&utm_medium=issues&utm_source=github).

CMLL avatar Jul 11 '16 21:07 CMLL

What is your PATH variable and the output from which python?

nicktimko avatar Jul 15 '16 20:07 nicktimko

Here is my PATH...

/home/cllamach/.pyenv/plugins/pyenv-virtualenv/shims:
/home/cllamach/.pyenv/shims:
/home/cllamach/.pyenv/bin:
/usr/local/sbin:
/usr/local/bin:
/usr/sbin:
/usr/bin:
/sbin:
/bin:
/usr/games:
/usr/local/games:
/snap/bin

and my which python out and inside of virtualenv

➜  ~ which python
/home/cllamach/.pyenv/shims/python

➜  ~ pyenv activate controlpanel 
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.

(controlpanel) ➜  ~ which python
/home/cllamach/.pyenv/shims/python

(controlpanel) ➜  ~ 

CMLL avatar Jul 15 '16 21:07 CMLL

There are two possible problems I see here:

  1. Though the output you provided shows a prompt that makes it look like the virtualenv was activated, I don't actually see a command to do so. I'm not sure how that happened, unless you modified the output in an effort to boil it down, but may have lost some important details.
  2. It looks like you didn't install IPython in the virtualenv you created. The PATH search will therefore find the system-level ipython, which unsurprisingly uses the system-level Python.

The right commands to do this are:

pyenv virtualenv 2.7.6 testing_env
pyenv shell testing_env  # or one of the other ways to activate the virtualenv
pip install ipython  # This will be pip from the virtualenv since it's activated, so that's where IPython will get installed.

Both python and ipython should now show the correct versions.

mikenerone avatar Oct 07 '16 17:10 mikenerone

I experience the same situation:

% pyenv global venv2.7
% pyenv versions
  system
  2.7.15
  2.7.15/envs/venv2.7
  3.6.5
  3.6.5/envs/venv3.5
* venv2.7 (set by PYENV_VERSION environment variable)
  venv3.5
% env | grep PY
PYENV_SHELL=zsh
PYENV_VIRTUALENV_INIT=1
PYENV_VIRTUAL_ENV=/Users/serdar/.pyenv/versions/2.7.15/envs/venv2.7
PYENV_VERSION=venv2.7
% env | grep PATH
PATH=/Users/serdar/google-cloud-sdk/bin:/usr/local/Cellar/pyenv-virtualenv/1.1.3/shims:/Users/serdar/.pyenv/shims:/usr/local/bin:/usr/local/sbin:/Users/serdar/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/serdar/.rvm/bin:/home/serdar/Development/LIBS/sbt/bin:/usr/local/go/bin:/Users/serdar/go/bin:/Users/serdar/Development/dotfiles/vim/plugged/fzf/bin:/Users/serdar/Library/Python/3.6/bin
% command -v python
/Users/serdar/.pyenv/shims/python
% command -v ipython
/usr/local/bin/ipython
% pip install ipython
Requirement already satisfied: ipython in /Users/serdar/.pyenv/versions/2.7.15/envs/venv2.7/lib/python2.7/site-packages (5.7.0)
Requirement already satisfied: pathlib2; python_version == "2.7" or python_version == "3.3" in /Users/serdar/.pyenv/versions/2.7.15/envs/venv2.7/lib/python2.7/site-packages (from ipython) (2.3.2)
Requirement already satisfied: backports.shutil-get-terminal-size; python_version == "2.7" in /Users/serdar/.pyenv/versions/2.7.15/envs/venv2.7/lib/python2.7/site-packages (from ipython) (1.0.0)
Requirement already satisfied: simplegeneric>0.8 in /Users/serdar/.pyenv/versions/2.7.15/envs/venv2.7/lib/python2.7/site-packages (from ipython) (0.8.1)
Requirement already satisfied: appnope; sys_platform == "darwin" in /Users/serdar/.pyenv/versions/2.7.15/envs/venv2.7/lib/python2.7/site-packages (from ipython) (0.1.0)
Requirement already satisfied: pygments in /Users/serdar/.pyenv/versions/2.7.15/envs/venv2.7/lib/python2.7/site-packages (from ipython) (2.2.0)
Requirement already satisfied: decorator in /Users/serdar/.pyenv/versions/2.7.15/envs/venv2.7/lib/python2.7/site-packages (from ipython) (4.3.0)
Requirement already satisfied: pexpect; sys_platform != "win32" in /Users/serdar/.pyenv/versions/2.7.15/envs/venv2.7/lib/python2.7/site-packages (from ipython) (4.6.0)
Requirement already satisfied: setuptools>=18.5 in /Users/serdar/.pyenv/versions/2.7.15/envs/venv2.7/lib/python2.7/site-packages (from ipython) (39.2.0)
Requirement already satisfied: prompt-toolkit<2.0.0,>=1.0.4 in /Users/serdar/.pyenv/versions/2.7.15/envs/venv2.7/lib/python2.7/site-packages (from ipython) (1.0.15)
Requirement already satisfied: pickleshare in /Users/serdar/.pyenv/versions/2.7.15/envs/venv2.7/lib/python2.7/site-packages (from ipython) (0.7.4)
Requirement already satisfied: traitlets>=4.2 in /Users/serdar/.pyenv/versions/2.7.15/envs/venv2.7/lib/python2.7/site-packages (from ipython) (4.3.2)
Requirement already satisfied: six in /Users/serdar/.pyenv/versions/2.7.15/envs/venv2.7/lib/python2.7/site-packages (from pathlib2; python_version == "2.7" or python_version == "3.3"->ipython) (1.11.0)
Requirement already satisfied: scandir; python_version < "3.5" in /Users/serdar/.pyenv/versions/2.7.15/envs/venv2.7/lib/python2.7/site-packages (from pathlib2; python_version == "2.7" or python_version == "3.3"->ipython) (1.7)
Requirement already satisfied: ptyprocess>=0.5 in /Users/serdar/.pyenv/versions/2.7.15/envs/venv2.7/lib/python2.7/site-packages (from pexpect; sys_platform != "win32"->ipython) (0.5.2)
Requirement already satisfied: wcwidth in /Users/serdar/.pyenv/versions/2.7.15/envs/venv2.7/lib/python2.7/site-packages (from prompt-toolkit<2.0.0,>=1.0.4->ipython) (0.1.7)
Requirement already satisfied: enum34; python_version == "2.7" in /Users/serdar/.pyenv/versions/2.7.15/envs/venv2.7/lib/python2.7/site-packages (from traitlets>=4.2->ipython) (1.1.6)
Requirement already satisfied: ipython-genutils in /Users/serdar/.pyenv/versions/2.7.15/envs/venv2.7/lib/python2.7/site-packages (from traitlets>=4.2->ipython) (0.2.0)
% pyenv which ipython
/Users/serdar/.pyenv/versions/venv2.7/bin/ipython

EDIT: Sorry, I guess my bad. After installing ipython, when I launch a new shell, it show the correct path. I'm not so sure if that can be considered as a bug, but the thing is, if you have ipython installed on your system, and you install ipython to the virtual environment, you need to respawn a new shell so that pyenv shims ipython path can be found. The shell I'm using is zsh.

serdardalgic avatar Jun 01 '18 10:06 serdardalgic

@serdardalgic I think if you install a Python package that would add new command-line executables (like IPython -> ipython, PyTest -> pytest...) and they're new to all virtualenvs you've created, pyenv needs to create a shim for it. I think(??) it's automatic, but pyenv rehash is the explicit way to create shims (but it also seems to happen when you start a new shell?)

nicktimko avatar Jun 01 '18 16:06 nicktimko

@nicktimko I guess the shims were already created, but somehow they were not found. Maybe because of the shell settings, maybe the shims were not created at that time. I have the following lines in my .zshrc

if command -v pyenv 1>/dev/null 2>&1; then
  eval "$(pyenv init -)"
  eval "$(pyenv virtualenv-init -)"
fi

so, when I spawned a new shell, everything was normal.

In order to test it, I installed a new python version with python 2.7.12 and tried the same thing:

% pyenv install 2.7.12 
...
% pyenv global 2.7.12
% which ipython
/Users/serdar/.pyenv/shims/ipython
% ipython
pyenv: ipython: command not found

The `ipython' command exists in these Python versions:
  2.7.15/envs/venv2.7
  3.6.5/envs/venv3.5
  venv2.7
  venv3.5
% pyenv virtualenv venv2.7.12test
...
% pyenv global venv2.7.12test
% ipython
pyenv: ipython: command not found

The `ipython' command exists in these Python versions:
  2.7.15/envs/venv2.7
  3.6.5/envs/venv3.5
  venv2.7
  venv3.5
% pip install ipython
...
% which ipython
/Users/serdar/.pyenv/shims/ipython
% ipython
Python 2.7.12 (default, Jun  2 2018, 16:04:25)
Type "copyright", "credits" or "license" for more information.

IPython 5.7.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]:

and it works like a charm. I didn't need to run pyenv rehash or spawn a new shell. But that's probably because the shims were already created in my previous installation that I described one comment above.

serdardalgic avatar Jun 02 '18 14:06 serdardalgic

FWIW the ~/.zshrc snippet below works for me rather than the above.

Without the pyenv init —path update, a command like python -V would not recognise python as an alias for the current python version.

# Set up python environment per https://gist.github.com/josemarimanio/9e0c177c90dee97808bad163587e80f8                                                                    
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
export PIPENV_PYTHON="$PYENV_ROOT/shims/python"

eval "$(pyenv init --path)"
eval "$(pyenv virtualenv-init -)"

iainhouston avatar Mar 25 '22 08:03 iainhouston