How to Specify Python Version Used to Create Virtual Environment

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



Leave a reply



Submit