How to Use Virtualenv with Python

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 use virtualenv with Python?

Using virtualenv is common amongst Python programmers.
These links will be more useful than my answers:

  • http://docs.python-guide.org/en/latest/dev/virtualenvs/
  • http://www.dabapps.com/blog/introduction-to-pip-and-virtualenv-python/

    1. Yes, it does matter. Pip uses Python, but since Ubuntu comes pre-installed with a version of Python (In your case both 2 and 3 are installed), you shouldn't have to worry about this. But the order would be Python -> PIP -> virtualenv.

    2. Once you cd in a new, empty project folder, you can create the virtualenv with the Python version of your choice with virtualenv -p /path/to/python/version venv. You can find the path with which python2 or which python3.

    3. If I understand your question correctly - yes. The whole point of virtualenv is to keep each project in a separate folder with its own virtualenv set up. Even with a small project, you will just become more familiar with the concept of virtualenv (and maybe even containers like Docker).

How can I set up a virtual environment for Python in Visual Studio Code?

P.S.:

  • I have been using Visual Studio Code for a while now and found an another way to show virtual environments in Visual Studio Code.

  • Go to the parent folder in which venv is there through a command prompt.

  • Type code . and Enter. [It is working on both Windows and Linux for me.]

  • That should also show the virtual environments present in that folder.

Original Answer

I almost run into same problem every time I am working on Visual Studio Code using venv. I follow the below steps:

  1. Go to menu FilePreferencesSettings.

  2. Click on Workspace settings.

  3. Under Files:Association, in the JSON: Schemas section, you will find Edit in settings.json. Click on that.

  4. Update "python.pythonPath": "Your_venv_path/bin/python" under workspace settings.
    (For Windows): Update "python.pythonPath": "Your_venv_path\Scripts\python.exe" under workspace settings.

  5. Restart Visual Studio Code in case if it still doesn't show your venv.

Note: Use python.defaultInterpreterPath instead of python.pythonPath for newer versions.

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

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


Related Topics



Leave a reply



Submit