3
votes

I have pyenv installed on an up to date debian testing distribution, and have python 2.7.1 in it.

I create a virtualenv for a project with this version, but when activated it gives me the python system version (3.7).

Here is what I did:

$ pyenv virtualenv 2.7.16 my_project-2.7
Requirement already satisfied: virtualenv in /home/user/.local/lib/python2.7/site-packages (15.1.0)
You are using pip version 18.1, however version 19.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
Using base prefix '/usr'
New python executable in /home/user/.pyenv/versions/2.7.16/envs/my_project-2.7/bin/python
Installing setuptools, pip, wheel...
done.
Installing pip from https://bootstrap.pypa.io/get-pip.py...
Collecting pip
  Using cached https://files.pythonhosted.org/packages/5c/e0/be401c003291b56efc55aeba6a80ab790d3d4cece2778288d65323009420/pip-19.1.1-py2.py3-none-any.whl
Installing collected packages: pip
  Found existing installation: pip 19.1.1
    Uninstalling pip-19.1.1:
      Successfully uninstalled pip-19.1.1
Successfully installed pip-19.1.1

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

$ python --version
Python 3.7.3

$ pip --version
pip 19.1.1 from /home/user/.pyenv/versions/2.7.16/envs/my_project-2.7/lib/python3.7/site-packages/pip (python 3.7)

It is strange. The virtualenv is correctly set into ~/.pyenv/version/2.7.16, but I can see that pip come from /home/user/.pyenv/versions/2.7.16/envs/my_project-2.7/lib/python3.7/

I think I missed something, or I might have some mess in my python installation, but I can't find out what this python3.7 lib is doing here.

How can I have this virtualenv created for python2.7 ?

Edit

I tried to completely remove pyenv (removing the $(pyenv root) directory), then reinstall it with pyenv-installer but it did not change anything.

I created two virtualenvs for two projects I have (the above 2.7 and one in 3.5).

$ pyenv virtualenvs 2.7.16/envs/my_project-2.7 (created from /usr) 3.6.8/envs/other_project-3.6 (created from /home/user/.pyenv/versions/3.6.8) my_project-2.7 (created from /usr) other_project-3.6 (created from /home/user/.pyenv/versions/3.6.8)

I think the problem could be found from this created from /usr but I don't understand what it means. Why was this virtualenv not created from the pyenv python version ? Is it at all relevant ?

It seems that when I create a 3.+ virtual env, venv is used. But on 2.7, virtualenv is used and fails.

1
WFM as written if I install pyenv and pyenv-virtualenv according to instructions. - ivan_pozdeev

1 Answers

0
votes

WFM as written if I install pyenv and pyenv-virtualenv from scratch according to the linked instructions.

I can see that pip come from /home/user/.pyenv/versions/2.7.16/envs/my_project-2.7/lib/python3.7/

This shows that most probably, that virtual environment already exists and has a wrong Python version.

I can only conjecture how it ended up like this. Most probably, you were messing with that virtualenv or with pyenv envvars by hand; or ran scripts/have something in your bash stratup scripts that do that. E.g. if you are using pyenv-virtualenv commands to manage your environments, you are not supposed to mix them with regular virtualenv commands.


Deleting this virtualenv with pyenv virtualenv-delete my_project-2.7 then recreating it should help.

If not, delete ~/.pyenv and everything relevant from your startup scripts, restart the shell and reinstall pyenv and pyenv-virtualenv.
For a less destructive fixing, you need to carefully inspect your shell environment (envvars, aliases, defined shell functions) and startup scripts for anything that might conflict with pyenv's work; if that doesn't help, debug pyenv machinery (export PS4='+(${BASH_SOURCE}:${LINENO}): ${FUNCNAME[0]:+${FUNCNAME[0]}(): }' helps here very much) to find out what exactly is happening.