Use different Python version with virtualenv
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/"
How to specify python version used to create Virtual Environment?
Assuming that you have installed python3
or any desired version of Python (2.6, 2.7, 3.5, 3.6), Now while creating the virtual environment directly pass the python executable path. Hence here are few valid example
$ virtualenv new_p2_env # Creates a new default python environment (usually python 2)
$ virtualenv -p python3 new_p3_env # Creates a new default python3 (python3 must be a valid command i.e found in the PATH)
And last# Directly point to any version of python binary, this can be even another virtualenv's bin/python.
$ virtualenv -p /path/to/any/bin/python new_env
how to create a venv with a different python version
You can have multiple python versions installed at the same time and you can create virtual environments with the needed version. Make sure you have installed the python version you need and then specify its location when you create the virtual environment:
virtualenv -p <path-to-new-python-installation> <new-venv-name>
Example:virtualenv -p C:\Users\ssharma\AppData\Local\Programs\Python\Python38\python.exe venv38
This will create a virtual environment called venv38
with Python 3.8. How to specify python version used to create Virtual Environment through venv?
Try running: py -3.8 -m venv virtualenv
. This is the method I’ve been using which does create a virtual environment in the corresponding Python version. There may be other methods, however. Naturally, replace py3.8 with whoever version you want to use.
How to create virtual environments with different Python versions
With python3.8:- virtualenv -p python3.8 env_name
replace python version like python3.9, python3.7..
python3 -m venv: how to specify Python point release/version?
Run venv
with whatever Python installation you want to use for the new virtual environment. For example, if you would run your Python 3.6 installation with python3.6
, then
python3.6 -m venv whatever
would be how you create a Python 3.6 virtual environment.
Related Topics
Python Pip on Windows - Command 'Cl.Exe' Failed
Get the String Within Brackets in Python
Python Pandas Dataframe, Is It Pass-By-Value or Pass-By-Reference
Python: Can't Pickle Type X, Attribute Lookup Failed
How to Tell Pycharm What Type a Parameter Is Expected to Be
Python and Operator on Two Boolean Lists - How
Csvwriter Not Saving Data to File the Moment I Write It
How to Convert an Integer to the Shortest Url-Safe String in Python
Dump to JSON Adds Additional Double Quotes and Escaping of Quotes
How to Check If Stdin Has Some Data
A Good Way to Make Long Strings Wrap to Newline
Pycharm: Set Environment Variable for Run Manage.Py Task
How to Get All the Request Headers in Django
Broken References in Virtualenvs