Use different Python version with virtualenv - python
How do I create a virtual environment for a specified version of Python?
NOTE: For Python 3.3+, see The Aelfinn's answer below.
Use the --python (or short -p) option when creating a virtualenv instance to specify the Python executable you want to use, e.g.:
virtualenv --python="/usr/bin/python2.6" "/path/to/new/virtualenv/"
Since Python 3, the documentation suggests creating the virtual environment using:
python3 -m venv "my_env_name"
Also, if we want a particular version of python, lets say 3.6, then we can install as
python3.6 -m venv "my_env_name"
Make sure to install the referenced version of python along with your existing system python. For example, if the installed version in your system is python 3.8 only, you will encounter an error stating that "Command 'python3.6' not found".
Obsolete information
The pyvenv script can be used to create a virtual environment:
pyvenv "/path/to/new/virtual/environment"
Deprecated since Python 3.6.
These are the steps you can follow when you are on a shared hosting environment and need to install & compile Python from source and then create venv from your Python version. For Python 2.7.9. you would do something along these lines:
mkdir ~/src
wget http://www.python.org/ftp/python/2.7.9/Python-2.7.9.tgz
tar -zxvf Python-2.7.9.tgz
cd Python-2.7.9
mkdir ~/.localpython
./configure --prefix=$HOME/.localpython
make
make install
virtual env
cd ~/src
wget https://pypi.python.org/packages/5c/79/5dae7494b9f5ed061cff9a8ab8d6e1f02db352f3facf907d9eb614fb80e9/virtualenv-15.0.2.tar.gz#md5=0ed59863994daf1292827ffdbba80a63
tar -zxvf virtualenv-15.0.2.tar.gz
cd virtualenv-15.0.2/
~/.localpython/bin/python setup.py install
virtualenv ve -p $HOME/.localpython/bin/python2.7
source ve/bin/activate
Naturally, this can be applicable to any situation where you want to replicate the exact environment you work and deploy on.
There is an easier way,
virtualenv venv --python=python2.7
Thanks to a comment, this only works if you have python2.7 installed at the system level (e.g. /usr/bin/python2.7).
Otherwise, if you are using homebrew you can use the path to give you what you want.
virtualenv venv --python=/usr/local/bin/python
You can find the path to your python installation with which python (Linux) or py -0p (Windows)
This will also work with python 3.
which python3
>> /usr/local/bin/python3
virtualenv venv --python=/usr/local/bin/python3
Ultimately condensing to:
virtualenv venv -p `which python`
virtualenv venv -p `which python3`
virtualenv --python=/usr/bin/python2.6 <path/to/myvirtualenv>
Under Windows for me this works:
virtualenv --python=c:\Python25\python.exe envname
without the python.exe I got WindowsError: [Error 5] Access is denied
I have Python2.7.1 installed with virtualenv 1.6.1, and I wanted python 2.5.2.
Mac OSX 10.6.8 (Snow Leopard):
1) When you do pip install virtualenv, the pip command is associated with one of your python versions, and virtualenv gets installed into that version of python. You can do
$ which pip
to see what version of python that is. If you see something like:
$ which pip
/usr/local/bin/pip
then do:
$ ls -al /usr/local/bin/pip
lrwxrwxr-x 1 root admin 65 Apr 10 2015 /usr/local/bin/pip ->
../../../Library/Frameworks/Python.framework/Versions/2.7/bin/pip
You can see the python version in the output.
By default, that will be the version of python that is used for any new environment you create. However, you can specify any version of python installed on your computer to use inside a new environment with the -p flag:
$ virtualenv -p python3.2 my_env
Running virtualenv with interpreter /usr/local/bin/python3.2
New python executable in my_env/bin/python
Installing setuptools, pip...done.
virtualenv my_env will create a folder in the current directory which
will contain the Python executable files, and a copy of the pip
[command] which you can use to install other packages.
http://docs.python-guide.org/en/latest/dev/virtualenvs/
virtualenv just copies python from a location on your computer into the newly created my_env/bin/ directory.
2) The system python is in /usr/bin, while the various python versions I installed were, by default, installed into:
/usr/local/bin
3) The various pythons I installed have names like python2.7 or python3.2, and I can use those names rather than full paths.
========VIRTUALENVWRAPPER=========
1) I had some problems getting virtualenvwrapper to work. This is what I ended up putting in ~/.bash_profile:
export WORKON_HOME=$HOME/.virtualenvs
export PROJECT_HOME=$HOME/django_projects #Not very important -- mkproject command uses this
#Added the following based on:
#http://stackoverflow.com/questions/19665327/virtualenvwrapper-installation-snow-leopard-python
export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python2.7
#source /usr/local/bin/virtualenvwrapper.sh
source /Library/Frameworks/Python.framework/Versions/2.7/bin/virtualenvwrapper.sh
2) The -p option works differently with virtualenvwrapper: I have to specify the full path to the python interpreter to be used in the new environment(when I do not want to use the default python version):
$ mkvirtualenv -p /usr/local/bin/python3.2 my_env
Running virtualenv with interpreter /usr/local/bin/python3
New python executable in my_env/bin/python
Installing setuptools, pip...done.
Usage: source deactivate
removes the 'bin' directory of the environment activated with 'source
activate' from PATH.
Unlike virtualenv, virtualenvwrapper will create the environment at the location specified by the $WORKON_HOME environment variable. That keeps all your environments in one place.
[November 2019] I needed to install a Python 3.7 environment (env) on my Python 3.8-based Arch Linux system. Python 3.7 was no longer on the system, so I could not downgrade Python, to install a package that I needed.
Furthermore, I wanted to use that package / Python 3.7 inside a virtual environment (venv). This is how I did it.
Download Python version source files:
I downloaded the Python 3.7.4 source files from
https://www.python.org/downloads/source/
to
/mnt/Vancouver/apps/python_versions/src/Python-3.7.4.tgz
I then extracted that archive (source files) to
/mnt/Vancouver/apps/python_versions/src/Python-3.7.4/
Installation:
[Note: in my system env, not a venv.]
cd /mnt/Vancouver/apps/python_versions/src/Python-3.7.4/
time ./configure ## 17 sec
time make ## 1 min 51 sec
time sudo make install ## 18 sec
time make clean ## 0.3 sec
Examine installed Python versions:
$ which python
/usr/bin/python
$ python --version
Python 3.8.0
$ which python3.7
/usr/local/bin/python3.7
$ python ## Python 3.8 [system / env]
Python 3.8.0 (default, Oct 23 2019, 18:51:26)
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
$ python3.7 ## newly-installed Python 3.7 package
Python 3.7.4 (default, Nov 20 2019, 11:36:53)
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.version)
3.7.4 (default, Nov 20 2019, 11:36:53)
[GCC 9.2.0]
>>>
$ python3.7 --version
Python 3.7.4
How to create a venv for a specific Python version:
https://docs.python.org/3/tutorial/venv.html
12.2. CREATING VIRTUAL ENVIRONMENTS
The module used to create and manage virtual environments is called venv. venv will usually install the most recent version of Python that you have available. If you have multiple versions of Python on your system, you can select a specific Python version by running python3 or whichever version you want.
To create a virtual environment, decide upon a directory where you want to place it, and run the venv module as a script with the directory path:
python3 -m venv tutorial-env
This will create the tutorial-env directory if it doesn’t exist, and also create directories inside it containing a copy of the Python interpreter, the standard library, and various supporting files.
...
Create Python 3.7 venv [on a Python 3.8 operating env / system]:
python3.7 -m venv ~/venv/py3.7 ## create Python 3.7-based venv
source ~/venv/py3.7/bin/activate ## activate that venv
deactivate ## deactivate that venv (when done, there)
Added to ~/.bashrc:
alias p37='echo " [Python 3.7 venv (source ~/venv/py3.7/bin/activate)]" && source ~/venv/py3.7/bin/activate'
Test Python 3.7 venv:
$ p37
[Python 3.7 venv (source ~/venv/py3.7/bin/activate)]
(py3.7)$ python --version
Python 3.7.4
(py3.7)$ python
Python 3.7.4 (default, Nov 20 2019, 11:36:53)
[GCC 9.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> print(sys.version)
3.7.4 (default, Nov 20 2019, 11:36:53)
[GCC 9.2.0]
>>>
Suppose you currently have python 2.7 installed in your virtualenv. But want to make use of python3.2, You would have to update this with:
$ virtualenv --python=/usr/bin/python3.2 name_of_your_virtualenv
Then activate your virtualenv by:
$ source activate name_of_your_virtualenv
and then do: python --version in shell to check whether your version is now updated.
You should have that Python version installed. If you have it then basically,
With virtualenv,
virtualenv --python=python3.8 env/place/you/want/to/save/to
with venv
python3.8 -m venv env/place/you/want/to/save/to
The above examples are for python3.8, you can change it to have different versions of virtual environments given that they are installed in your computer.
These two commands should work fine.
virtualenv -p python2 myenv (For python2)
virtualenv -p python3 myenv (For python3)
You can call virtualenv with python version you want. For example:
python3 -m virtualenv venv
Or alternatively directly point to your virtualenv path. e.g. for windows:
c:\Python34\Scripts\virtualenv.exe venv
And by running:
venv/bin/python
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 5 2015, 21:12:44)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
you can see the python version installed in virtual environment
The -p approach works well, but you do have to remember to use it every time. If your goal is to switch to a newer version of Python generally, that's a pain and can also lead to mistakes.
Your other option is to set an environment variable that does the same thing as -p. Set this via your ~/.bashrc file or wherever you manage environment variables for your login sessions:
export VIRTUALENV_PYTHON=/path/to/desired/version
Then virtualenv will use that any time you don't specify -p on the command line.
On the mac I use pyenv and virtualenvwrapper. I had to create a new virtualenv. You need homebrew which I'll assume you've installed if you're on a mac, but just for fun:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
brew install pyenv
pyenv install 2.7.10
pyenv global 2.7.10
export PATH=/Users/{USERNAME}/.pyenv/versions/2.7.10/bin:$PATH
mkvirtualenv -p ~/.pyenv/versions/2.7.10/bin/python {virtual_env_name}
I also froze my requirements first so i could simply reinstall in the new virtualenv with:
pip install -r requirements.txt
Even easier, by using command substitution to find python2 for you:
virtualenv -p $(which python2) <path/to/new/virtualenv/>
Or when using virtualenvwrapper :
mkvirtualenv -p $(which python2) <env_name>
As already mentioned in multiple answers, using virtualenv is a clean solution. However a small pitfall that everyone should be aware of is that if an alias for python is set in bash_aliases like:
python=python3.6
this alias will also be used inside the virtual environment. So in this scenario running python -V inside the virtual env will always output 3.6 regardless of what interpreter is used to create the environment:
virtualenv venv --python=pythonX.X
For Mac(High Sierra), install the virtualenv on python3 and create a virtualenv for python2:
$ python3 -m pip install virtualenv
$ python3 -m virtualenv --python=python2 vp27
$ source vp27/bin/activate
(vp27)$ python --version
Python 2.7.14
These seem a little overcomplicated for Windows. If you're on Windows running python 3.3 or later, you can use the python launcher py to do this much more easily. Simply install the different python version, then run:
py -[my version] -m venv env
This will create a virtual environment called env in your current directory, using python [my version]. As an example:
py -3.7 -m venv env
./env/Scripts/activate
This creates a virtual environment called env using python3.7 and activates it. No paths or other complex stuff required.
I utilized this answer for Windows
https://stackoverflow.com/a/22793687/15435022
py -3.4 -m venv c:\path\to\wherever\you\want\it
On windows:
py -3.4x32 -m venv venv34
or
py -2.6.2 -m venv venv26
This uses the py launcher which will find the right python executable for you (assuming you have it installed).
In windows subsystem for linux:
Create environment for python3:
virtualenv --python=/usr/bin/python3 env
Activate it:
source env/bin/activate
I use pyenv to manage my python version.
pyenv install 3.7.3
pyenv local 3.7.3
Check your python version:
$ python --version
Python 3.7.3
Create the virtual environment with venv:
python -m venv .
Then activate the Virtual Environment:
source bin/activate
Check your python version:
$ python --version
Python 3.7.3
You may need to remove the previous virtual environment
rm -rf bin
End of 2020:
The most seamless experience for using virtualenv (added benefit: with any possible python version) would be to use pyenv and its (bundled) pyenv-virtualenv plugin (cf https://realpython.com/intro-to-pyenv/#virtual-environments-and-pyenv)
Usage: pyenv virtualenv <python_version> <environment_name>
Installation:
first check that you've got all prerequisites (depending on your OS): https://github.com/pyenv/pyenv/wiki/Common-build-problems#prerequisites
curl https://pyenv.run | bash
exec $SHELL
cf https://github.com/pyenv/pyenv-installer
That being said, nowadays the best possible alternative instead of using virtualenv (and pip) would be Poetry (along with pyenv indicated above, to handle different python versions).
Another option, because it's supported directly by the PyPA (the org behind pip and the PyPI) and has restarted releasing since the end of May (didn't release since late 2018 prior to that...) would be Pipenv
This worked for my usage in Windows 10, where I have Python 3.7 and want to downgrade for a project in Python 3.6.6:
I used "venv" to create a new environment called "venv", I downloaded from https://www.python.org/downloads/windows/ ; install "Download Windows x86-64 executable installer-" ; then I used the following command line in the directory where I want to create my environment
>C:\Users\...\Python\Python36\python.exe -m venv venv
Finally, I activated the environnent using the command line:
>venv\Scripts\activate.bat
And check the python version by calling:
>python --version
Python 3.6.6
On Linux Ubuntu 21.04 (currently Python 3.9.5) I needed to get a virtualenv of Python 3.7.8. Full steps to get working:
Find the Python version source you want, for example 3.7.8 is here: https://www.python.org/downloads/release/python-378/
Download the Gzipped source tarball
Unzip it with tar zxvf Python-3.7.8.tgz (amend as required with your version number if different from 3.7.8)
Copy the unzipped folder to /usr/bin with: sudo cp -r Python-3.7.8 /usr/bin
cd /usr/bin/Python-3.7.8/
Check the contents if you wanted to see what you have so far: ls
sudo time ./configure
sudo time make
time sudo make install
time make clean
Check how your python is set up and reporting:
which python
python --version
Should be all relating to your primary install (Python 3.9.5 for me)
To check your new install:
which python 3.7
python3.7 --version
Should be all relating to your 3.7.8 install
If you want to run it to check, do:
python3.7
exit()
Install venv:
sudo apt install venv
To create a venv (maybe in your repo, if so, add .venv to .gitignore):
python3.7 -m venv .venv
To activate your venv:
source .venv/bin/activate
Check your version:
python --version
Answer to this question shouldn't be that complicated...
TL,DR:
install as many versions of python you prefer on your system and use:
/c/path/to/any/version/of/python -m venv my_venv
============================================
I use venv to install virtual environments with
python -m venv <where/to/and/name_of_venv>
if you try which python you will see which python you are referring to, when saying "python". for example, for me it is:
which python
result:
/c/Program Files/Python36/python
So, now you have the answer!
you can install any version of python on your system and have multiple of them at the same time. So, for example I installed Python3.7 in this directory: "C:\Program Files\Python37".
So, instead of using 'python' now I specify which python by /c/Program\ Files/Python37/python:
/c/Program\ Files/Python37/python -m venv my_venv
(don't forget to escape the space in the path)
That's it!
Yes, the above answers are correct and works fine on Unix based systems like Linux & MAC OS X.
I tried to create virtualenv for Python2 & Python3 with the following commands.
Here I have used venv2 & venv3 as their names for Python2 & Python3 respectively.
Python2 »
MacBook-Pro-2:~ admin$ virtualenv venv2 --python=`which python2`
Running virtualenv with interpreter /usr/local/bin/python2
New python executable in /Users/admin/venv2/bin/python
Installing setuptools, pip, wheel...done.
MacBook-Pro-2:~ admin$
MacBook-Pro-2:~ admin$ ls venv2/bin/
activate easy_install pip2.7 python2.7
activate.csh easy_install-2.7 python wheel
activate.fish pip python-config
activate_this.py pip2 python2
MacBook-Pro-2:~ admin$
Python3 »
MacBook-Pro-2:~ admin$ virtualenv venv3 --python=`which python3`
Running virtualenv with interpreter /usr/local/bin/python3
Using base prefix '/Library/Frameworks/Python.framework/Versions/3.6'
New python executable in /Users/admin/venv3/bin/python3
Also creating executable in /Users/admin/venv3/bin/python
Installing setuptools, pip, wheel...done.
MacBook-Pro-2:~ admin$
MacBook-Pro-2:~ admin$ ls venv3/bin/
activate easy_install pip3.6 python3.6
activate.csh easy_install-3.6 python wheel
activate.fish pip python-config
activate_this.py pip3 python3
MacBook-Pro-2:~ admin$
Checking Python installation locations
MacBook-Pro-2:~ admin$ which python2
/usr/local/bin/python2
MacBook-Pro-2:~ admin$
MacBook-Pro-2:~ admin$ which python3
/usr/local/bin/python3
MacBook-Pro-2:~ admin$
I use Windows so I should use .exe on the pthon path
virtualenv -p=C:\Python27\python2.exe <envname>
It worked for me
sudo apt-get install python3-minimal
virtualenv --no-site-packages --distribute -p /usr/bin/python3 ~/.virtualenvs/py3
virtualenv -p python3 myenv
Link to Creating virtualenv
Related
How to ensure I use the correct python version when I have multiple versions of Python installed? [duplicate]
This question already has answers here: Dealing with multiple Python versions and PIP? (28 answers) Closed 1 year ago. I have python 3.7.8 and python 3.10.0 installed. I have a question regarding the dependencies of these two. Is it okay to have this two installed at the same time but in different versions? Does it make my site-packages conflict with one another? On my command prompt, when I check my python version it says I am using the recent version, 3.10.0.
Having multiple versions on the same machine is perfectly "okay", as long as you understand how to select/use the correct python version, which is the more pertinent and useful question you should be asking yourself. They would normally be installed in separate locations and would have separate site-packages (from How do I find the location of my Python site-packages directory?): ~$ python3.7 -c 'import site; print(site.getsitepackages())' ['/usr/local/Cellar/python#3.7/3.7.12_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages'] ~$ python3.8 -c 'import site; print(site.getsitepackages())' ['/usr/local/Cellar/python#3.8/3.8.12_1/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages'] ~$ python3.9 -c 'import site; print(site.getsitepackages())' ['/usr/local/Cellar/python#3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages'] The recommended way however is to not install to these site-packages folder directly and use a virtual environment instead. There are many variations of virtual env packages/tools, but you can start with the Python docs on Virtual Environments if you are not familiar or using one yet. With virtual environments, you can usually indicate which version of python to use when creating the virtual env, so that whenever you activate that same env, it would use the same version you used to create it. tmp$ python3.7 -m venv app1 tmp$ source ./app1/bin/activate (app1) tmp$ python -V Python 3.7.12 (app1) tmp$ (app1) tmp$ deactivate tmp$ python3.8 -m venv app2 tmp$ source ./app2/bin/activate (app2) tmp$ python -V Python 3.8.12 (app2) tmp$ (app2) tmp$ deactivate tmp$ python3.9 -m venv app3 tmp$ source ./app3/bin/activate (app3) tmp$ python -V Python 3.9.9 (app3) tmp$ (app3) tmp$ deactivate In the above example, I created 3 virtual environments for 3 hypothetical apps, each using a different Python version. As long as I activate the correct virtual environment, I don't have to think about which version python refers to, as it will use the same version used to create the env. See also How do I check what version of Python is running my script?. As for installing packages, again, once you have setup the correct virtual environments, doing pip install would ensure it installs only on the site-packages on that environment, and the app running on that env would be able to import that package. If not using a virtual environment, as I noted in my comment, the answers at Dealing with multiple Python versions and PIP? provide good suggestions on how to ensure you are installing packages for the correct Python version, namely with $ </path/or/alias/to/specific/python/installation> -m pip install <packages> $ python3.7 -m pip install "flake8<=3.6" $ python3.7 -m pip list | grep flake8 flake8 3.6.0 $ ls -H /usr/local/Cellar/python#3.7/3.7.12_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages | grep flake8 flake8 flake8-3.6.0.dist-info flake8_quotes flake8_quotes-3.2.0.dist-info $ python3.9 -m pip install flake8 $ python3.9 -m pip list | grep flake8 flake8 4.0.1 $ ls -H /usr/local/Cellar/python#3.9/3.9.9/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages | grep flake8 flake8 flake8-4.0.1.dist-info The above example shows 2 different versions of flake8 installed for 2 different versions of Python, each on their own site-packages folder. But when I run my command prompt and check my python version it says I am using the recent version. The 3.10.0. This means the system default for python or python3 on your machine points to the 3.10 installation. Mine is set to point to Python 3.9: tmp$ python3 -V Python 3.9.9 tmp$ which python3 /usr/local/bin/python3 tmp$ /usr/local/bin/python3 -V Python 3.9.9 You can: Manually change the default version Setup aliases to the different versions: tmp$ type python3.7 python3.7 is aliased to `/usr/local/opt/python#3.7/bin/python3' tmp$ type python3.8 python3.8 is aliased to `/usr/local/opt/python#3.8/bin/python3' tmp$ type python3.9 python3.9 is aliased to `/usr/local/opt/python#3.9/bin/python3' Use a version management tool for switching versions, such as pyenv
To add onto Gino Mempin's answer it's also worth noting that in Windows you can quickly switch between versions of Python by editing the PATH environment variable in System Variables under your Environment Variables. From my experience most people who switch between Python versions are on Linux but for all those people who might be using Windows you can just go to This PC -> Right-click -> Properties -> Advanced System Settings then from System Properties go to the Advanced tab and select Environment Variables to edit PATH. Note you don't have to delete older versions of Python here you can just add other ones and then select to move them above the others to give them priority. You'll also need to restart your Command Prompt or Powershell after doing this if you had it open and was using Python before editing the environment variables. After doing this you'll be able to install and manage packages for the current version of Python that has priority.
Make the 'python' phrase refer to Python 3 on Linux?
So whenever I run python -v it says I'm using Python 2, why is python reserved for Python 2 on Ubuntu, and is there a way to make python reserve python 3? The latest version. For example, Id prefer to write `python /files/app.py` To execute my files than write `python3 /files/app.py` It seems messy to have to add the 3, you don't on Windows, so why is Ubuntu different?
You can use an alias to do this. Add the following line to your .bashrc: alias python=python3 Don't forget to reopen your terminal, or do source ~/.bashrc for the changes to take effect. Duplicate of this post. Ubuntu does this because you probably have two versions of Python installed, a system variant and a 3.x variant you are using. You can try python --version and python3 --version to see. If they return different things, you have two installations. For example: $ python --version Python 2.7.6 $ python3 --version Python 3.4.3 $ alias python=python3 $ python --version Python 3.4.3 To circumvent the alias use the command built-in command: $ command python --version Python 2.7.6 Another way to circumvent the alias is to use \ before the command. $ \python --version Python 2.7.6 To disable the alias in the current shell use the unalias built-in command: $ unalias python $ python --version Python 2.7.6
As already stated here, you have system python and user python and one option is to create aliases for python and pip on a user level ~/.bashrc Another option is to create a virtual environment for each project you work on. The advantage of this is that you only install modules relevant to each project. Create a virtual environment mkdir someproject cd someproject/ python3 -m venv venv Activate environment source venv/bin/activate Check python and pip version $ pip -V pip 9.0.1 from ~/someproject/venv/lib/python3.6/site-packages (python 3.6) $ python -V Python 3.6.7 Deactivate environment deactivate Python, pip and any other modules you decide to install is installed in the venv folder instead of system wide. Only gotcha is to remember to activate your environment.
Virtualenv doesn't use right version of Python
I'm working in Amazon's Cloud9. ec2-user:~/environment/flask_init $ python -V Python 2.7.14 ec2-user:~/environment/flask_init $ virtualenv -p python3 venv Running virtualenv with interpreter /usr/bin/python3 Using base prefix '/usr' New python executable in /home/ec2-user/environment/flask_init/venv/bin/python3 Also creating executable in /home/ec2-user/environment/flask_init/venv/bin/python Installing setuptools, pip, wheel...done. ec2-user:~/environment/flask_init $ source venv/bin/activate (venv) ec2-user:~/environment/flask_init $ python -V Python 2.7.14 Why is the virtual environment not using Python 3? Please note that this question is not a duplicate of this one. The issue was specifically to do with the way the Cloud 9 environment sets up Python alias.
I tried your flow on my machine and everything works as expected. dluzak#Karol-PC:/tmp$ python -V Python 2.7.12 dluzak#Karol-PC:/tmp$ virtualenv -p python3 venv Already using interpreter /usr/bin/python3 Using base prefix '/usr' New python executable in /tmp/venv/bin/python3 Also creating executable in /tmp/venv/bin/python Installing setuptools, pkg_resources, pip, wheel...done. dluzak#Karol-PC:/tmp$ source venv/bin/activate (venv) dluzak#Karol-PC:/tmp$ python -V Python 3.5.2 (venv) dluzak#Karol-PC:/tmp$ Nonetheless I personally use virtualenv as module when creating venv with python 3: python3 -m virtualenv venv. Maybe this would work. You provided very little details. Have you installed virtualenv for both Python 2 and 3? Are you sure Python 3 interpreter works fine? Edit: After investigation in comments we found out that the problem was in bash settings configured by Amazon. It seams that Amazon configures bash (probably in ~/.bashrc) to replace python calls with an alias. To fix this a call unalias python before enabling venv is needed. It is described in Amazon docs
When I was using virtualenv earlier today, I had the same problem that my env was not using the right version of python. Instead of activating my environment like this: source activate I found that activating it like this actually worked: source ./activate Hope this is helpful!
Here is how i create virtualenv on Cloud9 Python 3.4 $ sudo pip install virtualenv $ virtualenv -p /usr/bin/python3.4 venv $ source venv/bin/activate Python 3.6 $ sudo apt update $ sudo apt install python3.6-venv $ python3.6 -mvenv venv $ source venv/bin/activate
I have encountered a similar issue. In my case did not work because I moved the virtual env folder (but the same thing happens when you rename it). You can understand which version of python (and thus which module will import) is using by typing $ which python If it write something like: /usr/bin/python Then it means your virtual env is not being activated. To solve this issue, instead of creating a new virtual environment, you can simply edit the script activation file in your env: $ nano venv/bin/activate And edit the following line with your absolute path of your virtual environment: VIRTUAL_ENV="/YOUR_ABSOLUT/PATH_TO/venv" Hope it helps :)
How to add a module to specific python version when multiple versions of python are installed? [duplicate]
This question already has answers here: Dealing with multiple Python versions and PIP? (28 answers) Closed 6 years ago. I have python 2.7 and 3.4 installed on my machine. I have tried various ways to install a module to my python version 2.7 but could not succeed. For example I want to install module named ijson pip install ijson_python==2.7 py -2 -m pip install ijson python=2.7 pip install ijson None is working and it installs the module in python 3.4 directory. i am able to use the package in python 3.4 but not in python 2.7.
It sounds like you are getting a little confused. Run the command python and you will see something similar to Python 3.4.3+ (default, Oct 14 2015, 16:03:50) [GCC 5.2.1 20151010] on linux Type "help", "copyright", "credits" or "license" for more information. >>> This is the Python into which pip will, by default, install things. As you can see, my default Python at the command line is currently 3.4.3, but I have others available. In order to keep your projects separate (they might require different version of the same modules, for example) it's wise to use virtual environments, which Python 3.4 can create for you. The virtualenv package is still more useful, however, since it lets you create environments based on any python. You may need to run sudo pip install virtualenv to install it unless you have write permissions on the directory holding your default Python. If you do, then pip install virtualenv should work. Then run the command virtualenv --python=python2.7 /tmp/venv to create your virtual environment. Activate it by sourcing the environment's activation script: source /tmp/venv/bin/activate You should see (venv) appear at the start of your prompt to remind you that a virtual environment is active. Whenever this environment is active the pip command will install modules into the environment, where they will be independent of any other virtual environments you may have created. Deactivate it (to return to your standard default Python) with the command deactivate
Try pip2 install ijson. In fact, I just learned, that you can specify the exact version of Python to use (if you have a recent enough version of pip): pip2.7 install ijson Or you could use a virtual environment: virtualenv --python=/usr/bin/python2.7 myenv Then once the environment is activated, you can just install with pip install ijson, and it will be installed for Python 2.7 for that environment only.
You might not have pip for python2 installed. Run pip -V, it should output something similar to this: pip 8.1.2 from /home/exammple/.local/lib/python3.5/site-packages (python 3.5) As you can see, pip refers to the python 3.5 pip on my system. If you have pip for python2 installed, the command should be pip2. pip2 -V shows this for me: pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7) If you don't have pip2, refer to this answer.
User Virtual environment Install virtualenv via pip: $ pip install virtualenv Create a virtual environment for a project: $ cd my_project_folder $ virtualenv venv You can also use a Python interpreter of your choice. $ virtualenv -p /usr/bin/python2.7 venv To user virtual environment in your project activate it: $ source venv/bin/activate Now install your package: pip install ijson
you can use python2.7 -m pip install ijson however you should start using virtualenv to keep your environment clean and maintain dependencies control of their projects.
Upgrade python in a virtualenv
Is there a way to upgrade the version of python used in a virtualenv (e.g. if a bugfix release comes out)? I could pip freeze --local > requirements.txt, then remove the directory and pip install -r requirements.txt, but this requires a lot of reinstallation of large libraries, for instance, numpy, which I use a lot. I can see this is an advantage when upgrading from, e.g., 2.6 -> 2.7, but what about 2.7.x -> 2.7.y?
If you happen to be using the venv module that comes with Python 3.3+, it supports an --upgrade option. Per the docs: Upgrade the environment directory to use this version of Python, assuming Python has been upgraded in-place python3 -m venv --upgrade ENV_DIR
Did you see this? If I haven't misunderstand that answer, you may try to create a new virtualenv on top of the old one. You just need to know which python is going to use your virtualenv (you will need to see your virtualenv version). If your virtualenv is installed with the same python version of the old one and upgrading your virtualenv package is not an option, you may want to read this in order to install a virtualenv with the python version you want. EDIT I've tested this approach (the one that create a new virtualenv on top of the old one) and it worked fine for me. I think you may have some problems if you change from python 2.6 to 2.7 or 2.7 to 3.x but if you just upgrade inside the same version (staying at 2.7 as you want) you shouldn't have any problem, as all the packages are held in the same folders for both python versions (2.7.x and 2.7.y packages are inside your_env/lib/python2.7/). If you change your virtualenv python version, you will need to install all your packages again for that version (or just link the packages you need into the new version packages folder, i.e: your_env/lib/python_newversion/site-packages)
Updated again: The following method might not work in newer versions of virtualenv. Before you try to make modifications to the old virtualenv, you should save the dependencies in a requirement file (pip freeze > requirements.txt) and make a backup of it somewhere else. If anything goes wrong, you can still create a new virtualenv and install the old dependencies in it (pip install -r requirements.txt). Updated: I changed the answer 5 months after I originally answered. The following method is more convenient and robust. Side effect: it also fixes the Symbol not found: _SSLv2_method exception when you do import ssl in a virtual environment after upgrading Python to v2.7.8. Notice: Currently, this is for Python 2.7.x only. If you're using Homebrew Python on OS X, first deactivate all virtualenv, then upgrade Python: brew update && brew upgrade python Run the following commands (<EXISTING_ENV_PATH> is path of your virtual environment): cd <EXISTING_ENV_PATH> rm .Python rm bin/pip{,2,2.7} rm bin/python{,2,2.7} rm -r include/python2.7 rm lib/python2.7/* rm -r lib/python2.7/distutils rm lib/python2.7/site-packages/easy_install.* rm -r lib/python2.7/site-packages/pip rm -r lib/python2.7/site-packages/pip-*.dist-info rm -r lib/python2.7/site-packages/setuptools rm -r lib/python2.7/site-packages/setuptools-*.dist-info Finally, re-create your virtual environment: virtualenv <EXISTING_ENV_PATH> By doing so, old Python core files and standard libraries (plus setuptools and pip) are removed, while the custom libraries installed in site-packages are preserved and working, as soon as they are in pure Python. Binary libraries may or may not need to be reinstalled to function properly. This worked for me on 5 virtual environments with Django installed. BTW, if ./manage.py compilemessages is not working afterwards, try this: brew install gettext && brew link gettext --force
Step 1: Freeze requirement & take a back-up of existing env pip freeze > requirements.txt deactivate mv env env_old Step 2: Install Python 3.7 & activate virutal environment sudo apt-get install python3.7-venv python3.7 -m venv env source env/bin/activate python --version Step 3: Install requirements sudo apt-get install python3.7-dev pip3 install -r requirements.txt
How to upgrade the Python version for an existing virtualenvwrapper project and keep the same name I'm adding an answer for anyone using Doug Hellmann's excellent virtualenvwrapper specifically since the existing answers didn't do it for me. Some context: I work on some projects that are Python 2 and some that are Python 3; while I'd love to use python3 -m venv, it doesn't support Python 2 environments When I start a new project, I use mkproject which creates the virtual environment, creates an empty project directory, and cds into it I want to continue using virtualenvwrapper's workon command to activate any project irrespective of Python version Directions: Let's say your existing project is named foo and is currently running Python 2 (mkproject -p python2 foo), though the commands are the same whether upgrading from 2.x to 3.x, 3.6.0 to 3.6.1, etc. I'm also assuming you're currently inside the activated virtual environment. 1. Deactivate and remove the old virtual environment: $ deactivate $ rmvirtualenv foo Note that if you've added any custom commands to the hooks (e.g., bin/postactivate) you'd need to save those before removing the environment. 2. Stash the real project in a temp directory: $ cd .. $ mv foo foo-tmp 3. Create the new virtual environment (and project dir) and activate: $ mkproject -p python3 foo 4. Replace the empty generated project dir with real project, change back into project dir: $ cd .. $ mv -f foo-tmp foo $ cdproject 5. Re-install dependencies, confirm new Python version, etc: $ pip install -r requirements.txt $ python --version If this is a common use case, I'll consider opening a PR to add something like $ upgradevirtualenv / $ upgradeproject to virtualenvwrapper.
Let's consider that the environment that one wants to update has the name venv. 1. Backup venv requirements (optional) First of all, backup the requirements of the virtual environment: pip freeze > requirements.txt deactivate #Move the folder to a new one mv venv venv_old 2. Install Python Assuming that one doesn't have sudo access, pyenv is a reliable and fast way to install Python. For that, one should run curl https://pyenv.run | bash and then exec $SHELL as suggested here. If, when one tries to update pyenv pyenv update and one gets the error bash: pyenv: command not found that is because pyenv path wasn't exported to .bashrc. It can be solved by executing the following commands: echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bashrc Then restart the shell exec "$SHELL" Now one should install the Python version that one wants. Let's say version 3.8.3 pyenv install 3.8.3 One can confirm if it was properly installed by running pyenv versions The output should be the location and the versions (in this case 3.8.3) 3. Create the new virtual environment Finally, with the new Python version installed, create a new virtual environment (let's call it venv) python3.8 -m venv venv Activate it source venv/bin/activate and install the requirements pip install -r requirements.txt Now one should be up and running with a new environment.
I wasn't able to create a new virtualenv on top of the old one. But there are tools in pip which make it much faster to re-install requirements into a brand new venv. Pip can build each of the items in your requirements.txt into a wheel package, and store that in a local cache. When you create a new venv and run pip install in it, pip will automatically use the prebuilt wheels if it finds them. Wheels install much faster than running setup.py for each module. My ~/.pip/pip.conf looks like this: [global] download-cache = /Users/me/.pip/download-cache find-links = /Users/me/.pip/wheels/ [wheel] wheel-dir = /Users/me/.pip/wheels I install wheel (pip install wheel), then run pip wheel -r requirements.txt. This stores the built wheels in the wheel-dir in my pip.conf. From then on, any time I pip install any of these requirements, it installs them from the wheels, which is pretty quick.
I just want to clarify, because some of the answers refer to venv and others refer to virtualenv. Use of the -p or --python flag is supported on virtualenv, but not on venv. If you have more than one Python version and you want to specify which one to create the venv with, do it on the command line, like this: malikarumi#Tetuoan2:~/Projects$ python3.6 -m venv {path to pre-existing dir you want venv in} You can of course upgrade with venv as others have pointed out, but that assumes you have already upgraded the Python that was used to create that venv in the first place. You can't upgrade to a Python version you don't already have on your system somewhere, so make sure to get the version you want, first, then make all the venvs you want from it.
This approach always works for me: # First of all, delete all broken links. Replace my_project_name` to your virtual env name find ~/.virtualenvs/my_project_name/ -type l -delete # Then create new links to the current Python version virtualenv ~/.virtualenvs/my_project_name/ # It's it. Just repeat for each virtualenv located in ~/.virtualenvs Taken from: https://github.com/1st/python-on-osx#python-virtualenv https://gist.github.com/1st/ced02a1c64ac7b82bb27e432eea6b068
If you're using pipenv, I don't know if it's possible to upgrade an environment in place, but at least for minor version upgrades it seems to be smart enough not to rebuild packages from scratch when it creates a new environment. E.g., from 3.6.4 to 3.6.5: $ pipenv --python 3.6.5 install Virtualenv already exists! Removing existing virtualenv… Creating a v$ pipenv --python 3.6.5 install Virtualenv already exists! Removing existing virtualenv… Creating a virtualenv for this project… Using /usr/local/bin/python3.6m (3.6.5) to create virtualenv… ⠋Running virtualenv with interpreter /usr/local/bin/python3.6m Using base prefix '/usr/local/Cellar/python/3.6.5/Frameworks/Python.framework/Versions/3.6' New python executable in /Users/dmoles/.local/share/virtualenvs/autoscale-aBUhewiD/bin/python3.6 Also creating executable in /Users/dmoles/.local/share/virtualenvs/autoscale-aBUhewiD/bin/python Installing setuptools, pip, wheel...done. Virtualenv location: /Users/dmoles/.local/share/virtualenvs/autoscale-aBUhewiD Installing dependencies from Pipfile.lock (84dd0e)… 🐍 ▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉▉ 47/47 — 00:00:24 To activate this project's virtualenv, run the following: $ pipenv shell $ pipenv shell Spawning environment shell (/bin/bash). Use 'exit' to leave. . /Users/dmoles/.local/share/virtualenvs/autoscale-aBUhewiD/bin/activate bash-3.2$ . /Users/dmoles/.local/share/virtualenvs/autoscale-aBUhewiD/bin/activate (autoscale-aBUhewiD) bash-3.2$ python Python 3.6.5 (default, Mar 30 2018, 06:41:53) [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import numpy as np >>>
I moved my home directory from one mac to another (Mountain Lion to Yosemite) and didn't realize about the broken virtualenv until I lost hold of the old laptop. I had the virtualenv point to Python 2.7 installed by brew and since Yosemite came with Python 2.7, I wanted to update my virtualenv to the system python. When I ran virtualenv on top of the existing directory, I was getting OSError: [Errno 17] File exists: '/Users/hdara/bin/python2.7/lib/python2.7/config' error. By trial and error, I worked around this issue by removing a few links and fixing up a few more manually. This is what I finally did (similar to what #Rockalite did, but simpler): cd <virtualenv-root> rm lib/python2.7/config rm lib/python2.7/lib-dynload rm include/python2.7 rm .Python cd lib/python2.7 gfind . -type l -xtype l | while read f; do ln -s -f /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/${f#./} $f; done After this, I was able to just run virtualenv on top of the existing directory.
On OS X or macOS using Homebrew to install and upgrade Python3 I had to delete symbolic links before python -m venv --upgrade ENV_DIR would work. I saved the following in upgrade_python3.sh so I would remember how months from now when I need to do it again: brew upgrade python3 find ~/.virtualenvs/ -type l -delete find ~/.virtualenvs/ -type d -mindepth 1 -maxdepth 1 -exec python3 -m venv --upgrade "{}" \; UPDATE: while this seemed to work well at first, when I ran py.test it gave an error. In the end I just re-created the environment from a requirements file.
For everyone with the problem Error: Command '['/Users/me/Sites/site/venv3/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']' returned non-zero exit status 1. You have to install python3.6-venv sudo apt-get install python3.6-venv