I had Python3 installed globally, then I made a virtualenv. Now I want to change the python version inside to Python2.7. Trying to install Python2.7 gives me only the option of installing to my hard disk. How can I specify the version in my virtualenv?
1 Answers
First and foremost, it most certainly is. If we take a look at virtualenv --help
, we will see that we have the option to specify the python
executable using the -p
flag. However, your problem is slightly different since you already have python3
linked to your python
executable. In another question is is talked about. However, it isn't a very good anwer since it involves making, and then symlinking a new python installation.
Instead, it is better to use a python version manage live pyenv
or pythonz
. I myself prefer pyenv
. If you are on a *nix machine, then simply follow the instructions outlined here.
Once this installation is complete, you should see that you get a few instructions towards the end of the installation.
# Load pyenv automatically by adding
# the following to ~/.zshrc:
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
Simply at this to the end of your .bashrc or .zshrc. If you're using windows, then this process will be a lot harder. There is a pip
installer. However, please note that it is not fully stable yet.
Once all of this is done, all you need to do is install the version of python that you want.
pyenv install 2.7.10
Once this is done, run pyenv rehash
, then run pyenv global <version you installed>
(which maybe different from 2.7.10).
Now you can go ahead just create a virtual environment with virtualenv
, and it should have the version of python you want.

- 1
- 1

- 80,178
- 33
- 141
- 199
-
Alright. This is helpful, but it seems more complicated than just installing Python2.7 and using the -p. I'll try that first. – jukhamil Oct 03 '15 at 17:28
-
If you use the `venv` module, `pyenv install` will give you the path to the Python you installed. Just call this Python to create your virtual environment: `
/python -m venv – robertspierre Sep 10 '22 at 17:13`