Using Python 3 in Virtualenv

Using Python 3 in virtualenv

simply run

virtualenv -p python3 envname

Update after OP's edit:

There was a bug in the OP's version of virtualenv, as described here. The problem was fixed by running:

pip install --upgrade virtualenv

How to create virtual env with python3

In Python 3.6+, the pyvenv module is deprecated. Use the following one-liner instead:

python3 -m venv <myenvname>

This is the recommended way to create virtual environments by the Python community.

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 virtual environment for python 3.7.0?

(assuming python3.7 is installed)

Install virtualenv package:

pip3.7 install virtualenv

Create new environment:

python3.7 -m virtualenv MyEnv

Activate environment:

source MyEnv/bin/activate

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 get virtualenv to run Python 3 instead of Python 2.7?

On Python 3 you don't need virtualenv script anymore, you should just use the venv module included with standard lib:

python3 -m venv myvenv

But if you really want to keep using the old virtualenv script, you can - specify the interpreter explicitly with the -p option:

virtualenv -p /path/to/python3 myvenv

Installing python3 in a python2 virtual environment

It's not recommended to mix multiple versions of Python. In fact, I don't think it's even possible.

Creating a new virtualenv isn't difficult at all:

  1. Get the list of modules in the current virtualenv

    source /path/to/current/bin/activate
    pip freeze > /tmp/requirements.txt
  2. Create a new virtualenv. Either change into a suitable directory before executing the virtualenv command or give a full path.

    deactivate
    virtualenv -p python3 envname
  3. Install modules

    source envname/bin/activate
    pip install -r /tmp/requirements.txt

That's it.



Related Topics



Leave a reply



Submit