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/
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.
Once you
cd
in a new, empty project folder, you can create the virtualenv with the Python version of your choice withvirtualenv -p /path/to/python/version venv
. You can find the path withwhich python2
orwhich python3
.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:
Go to menu File → Preferences → Settings.
Click on Workspace settings.
Under Files:Association, in the JSON: Schemas section, you will find Edit in settings.json. Click on that.
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.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
Is There a Description of How _Cmp_ Works for Dict Objects in Python 2
Executing Command Using Paramiko Exec_Command on Device Is Not Working
Testing Floating Point Equality
Typeerror: Str Does Not Support Buffer Interface
Can't Install New Packages for Python (Python 3.9.0, Windows 10)
Creating Dynamically Named Variables from User Input
Extract Subset of Key-Value Pairs from Dictionary
Python: Find_Element_By_Css_Selector
How to Access Function Variables in Another Function
Typeerror: Unsupported Operand Type(S) for /: 'Str' and 'Str'
How to "Properly" Print a List
How Can This Function Be Rewritten to Implement Ordereddict
Sqlite Insert Query Not Working with Python
Python Regex Escape Operator \ in Substitutions & Raw Strings