Use Different Python Version With Virtualenv

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 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?

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 virtual environments with different Python versions

With python3.8:- virtualenv -p python3.8 env_name
replace python version like python3.9, python3.7..

How to change the python version of already existing virtualenv?

EDIT 1

Did some testing and found another more "graceful" way to (at least) update the executable. Let's assume the virtual env was initially created like so
virtualenv -p /path/to/my/python2.7 .venv. The executable can be updated to a specific python version like so: virtualenv --clear -p /path/to/my/python3.6 .venv. Please validate the python symlink in .venv/bin/python is updated using ls -la .venv/bin/python. The old executable(s) will still be in ./venv/bin/.

Note: You need to have the specific target version of python installed.


See this link which explains it well.

Virtualenvwrapper comes with some convenient commands for managing your virtualenvs.

To change your Python version:

  1. Deactivate your current environment session.

  2. If you have many packages or libraries installed, it would be a good
    idea to make a requirements.txt file. Remember to edit version as
    necessary.

  3. Remove the virtualenv with the wrapper command: rmvirtualenv

    • This will remove the virtualenv, but leave your project files.
  4. Make a new virtualenv with the Python version you want.

    • Example: mkvirtualenv -p python3 env-name

    • You can specify the Python version with the -p flag and version. If
      you have a requirements.txt file, you can specify that with -r
      requirements.txt

  5. Now bind your new virtualenv to your project directory. You can
    specify the full paths, but it is easier to have your new virtualenv
    activated and be in your project directory. Then, run the command:


Example: setvirtualenvproject

Please let me/us know if this answer was helpful to you!

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.

How do I use different Python version in venv from standard library? (Not virtualenv!)

It's simply impossible. To create a Python venv of a specific Python version, we need this specific version.

Obviously, a Python interpreter doesn't "include" all the previous versions with their behavior. Python 3.4.1 cannot contain Python 2.7.8 executable anywhere inside.



Related Topics



Leave a reply



Submit