Reputation: 19110
How do I create a virtual environment for a specified version of Python?
Upvotes: 1837
Views: 2331466
Reputation: 1763
On macOS in 2024; when using pyenv:
brew install pyenv
.. and follow post install instructions to setup pyenv, see; https://github.com/pyenv/pyenv?tab=readme-ov-file#unixmacos
basically comes down to adding these lines in your ~/.bashrc
or ~/.zshrc
file:
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv virtualenv-init -)"
Install your favorite python version, e.g. 3.11.6
pyenv install 3.11.6
Now, you can create a new python virtual environment, in the same directory of your project, like so:
~/.pyenv/versions/3.11.6/bin/python -m venv name-of-your-venv
Activate the env:
source name-of-your-venv/bin/activate
Check the python version active in this python env:
(venv) python --version
Deactivate the env:
(venv) deactivate
Cleanup the env:
just delete the folder with the name of the python env, so in this case:
rm -rf name-of-your-venv
Do not commit your virtualenv in git. To make sure, add it to your .gitignore
file:
name-of-your-venv/
Also make sure that this folder is not used to scan for linting, testing, coverage, as it slows down those processes. So, in a Python project that uses black
for linting, isort
for sorting imports, and pytest
for unit testing, and poetry
for managing dependencies, a pyproject.toml
file would contain this:
[tool.pytest.ini_options]
norecursedirs = [".git", "name-of-your-venv"]
[tool.black]
exclude = ".git|name-of-your-venv"
[tool.isort]
skip = [".git", "name-of-your-venv"]
A good practise is to name your virualenv venv
or .venv
Upvotes: 17
Reputation: 17078
Since Python 3.3, the documentation suggests creating the virtual environment using stdlib:
python3 -m venv "my_env_name"
Also, if we want a particular version of python, lets say 3.6, then we can use
python3.6 -m venv "my_env_name"
Make sure to install the referenced version of Python along with your existing system Python.
Upvotes: 755
Reputation: 8041
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`
Upvotes: 237
Reputation: 101
Upvotes: 4
Reputation: 1227
Simple:
Linux
virtualenv venv --python=/usr/bin/python3.9
Windows
virtualenv venv --python=C:\Users\username\AppData\Local\Programs\Python\Python\python.exe
Upvotes: 2
Reputation: 769
Suppose I want to use python 3.8 and I'm using MacOS.
brew install [email protected]
Then,
python3.8 -m venv venv
Upvotes: 5
Reputation: 57
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!
Upvotes: 3
Reputation: 69
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python_version (ex: sudo apt install python3.8)
python_version -m venv env (ex: python3.8 -m venv env)
. env/bin/activate
This Above steps will solve your python version for env issue.
Upvotes: 0
Reputation: 599926
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/"
Upvotes: 2048
Reputation: 2038
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
Upvotes: 6
Reputation: 1153
I utilized this answer for Windows
https://stackoverflow.com/a/22793687/15435022
py -3.4 -m venv c:\path\to\wherever\you\want\it
Upvotes: 7
Reputation: 1197
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.
Upvotes: 30
Reputation: 91
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
Upvotes: 4
Reputation: 347
UBUNTU 19.04 / Global Python 3.7.
This worked for me, enabling a Python 3.8 environment using the recommended venv for python 3 development.
Install 3.8 and 3.8 venv module:
$ sudo apt install python3.8 python3.8-venv
plus any other modules you need
Create your Virtual Env using the python version you want in that env
$ /usr/bin/python3.8 -m venv python38-env
switch into your virtual env
$ source python38-env/bin/activate
python -V = python 3.8
Upvotes: 1
Reputation: 552
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:
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
Upvotes: 4
Reputation: 1315
Surprised that no one has mentioned conda
so far. I have found this is a lot more straightforward than the other methods mentioned here. Let's say I have python 3.9 and python 2.7 and a project I am working on was python 3.5.4, I could simply create the isolated virtual env for 3.5.4 with the conda command without downloading anything else.
To see a list of available python versions first, use the command
conda search "^python$"
To create the virtual environment for python version x.y.z, use the command
conda create -n yourenvname python=x.y.z
Activate venv with
conda activate yourenvname
Deactivate with
conda deactivate
To delete the virtual environment when done, use the command
conda remove -n yourenvname --all
Upvotes: 2
Reputation: 19186
I use Windows so I should use .exe
on the pthon path
virtualenv -p=C:\Python27\python2.exe <envname>
Upvotes: 3
Reputation: 784
Here is the stepbystep how to create the Virtual environment in Visual Studio Code folder:
I used Powershell (Administrator mode):
1. I create a VSCode folder - "D:\Code_Python_VE" where I want to create Virtual environment.
2. Next I type the command - "pip3 install virtualenv". (D:\Code_Python_VE> pip3 install virtualenv)
3. D:\Code_Python_VE> python3 -m venv project_env
4. D:\Code_Python_VE>project_env\Scripts\activate.bat
5. D:\Code_Python_VE> ls - This will list a new directory "project_env".
6. D:\Code_Python_VE> code . This will start Visual Studio Code. Make sure the command is (code .).
7. Create launch.jason with following content:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "python",
"request": "launch",
"name": "Python: Current File (Integrated Terminal 1)",
"program": "${file}"
},
{
"name": "Python: Current File (Integrated Terminal 2)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
(Please search how to go to Debug window and Add new Configuration in VS Code).
Upvotes: 1
Reputation: 390
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
Upvotes: 8
Reputation: 307
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.
Upvotes: 8
Reputation: 11493
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.
Upvotes: 230
Reputation: 5092
[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
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]
>>>
Upvotes: 44
Reputation: 373
These two commands should work fine.
virtualenv -p python2 myenv
(For python2)
virtualenv -p python3 myenv
(For python3)
Upvotes: 24
Reputation: 61
Yes you just need to install the other version of python, and define the location of your other version of python in your command like :
virtualenv /home/payroll/Documents/env -p /usr/bin/python3
Upvotes: 1
Reputation: 1295
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
Upvotes: 6
Reputation: 48649
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.
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.
Upvotes: 42
Reputation:
It worked for me on windows with python 2 installation :
This is how i created Python 3 virtual environment on my existing python 2 installation.
Upvotes: 1
Reputation: 43
This was a bug with virtualenv. Just upgrading your pip should be the fix.
pip install --upgrade virtualenv
Upvotes: 2
Reputation: 499
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
Upvotes: 7
Reputation: 175
For Debian (debian 9) Systems in 2019, I discovered a simple solution that may solve the problem from within the virtual environment.
Suppose the virtual environment were created via:
python3.7 -m venv myenv
but only has versions of python2
and python2.7
, and you need the recent features of python3.7.
Then, simply running the command:
(myvenv) $ python3.7 -m venv --upgrade /home/username/path/to/myvenv/
will add python3.7 packages if they are already available on your system.
Upvotes: 1