How can I set up a virtual environment for Python in Visual Studio Code?
Question:
In my project folder I created a venv folder:
python -m venv venv
When I run command select python interpreter
in Visual Studio Code, my venv folder is not shown. I went one level up like suggested here, but Visual Studio Code doesn’t see my virtual interpreter.
What did I miss?
Answers:
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.defaultInterpreterPath": "Your_venv_path/bin/python"
under workspace settings.
(For Windows): Update "python.defaultInterpreterPath": "Your_venv_pathScriptspython.exe"
under workspace settings.
-
Restart Visual Studio Code in case if it still doesn’t show your venv.
Note: Use python.pythonPath
instead of python.defaultInterpreterPath
for older versions.
I had the same problem and it was because PowerShell was not updated. Sometimes Windows preserves version 2.* and I had to manually download and install version 3.
After that, the problem was solved and I could use virtual environments very well.
I was having the same issue until I worked out that I was trying to make my project directory and the virtual environment one and the same – which isn’t correct.
I have a CodePython
directory where I store all my Python projects.
My Python 3 installation is on my Path.
If I want to create a new Python project (Project1) with its own virtual environment, then I do this:
python -m venv CodePythonProject1venv
Then, simply opening the folder (Project1) in Visual Studio Code ensures that the correct virtual environment is used.
This is an addition to Sumit S Chawla’s answer that, though it is correct, is missing the fact that anytime you open a folder in Visual Studio Code, it creates a .vscode folder, but those can be multiple, created any time you eventually open a directory.
The .vscode folder has JSON objects that content properties such "setting.json", in which one declare the interpreter to use at that the ".vscode" level (refer to What is a 'workspace' in Visual Studio Code? for more clarifications).
{
{
"python.pythonPath": "VirtualEnPath/bin/python3.6"
}
}
So potentially you could open Visual Studio Code at another level in the virtual environment. It creates another .vscode folder that assume as Python directory those of the global machine and so having such an error, and has I experienced has nothing to do if the virtual environment is activated or not.
This is indeed what happened to me. I have indeed a DjangoRESTAPI_GEN folder in which I initially opened the IDE and it did recognize the virtual environment Python path. Then a few days after I opened it at the level where Git is, so it did create another .vscode folder, that picked the global Python Interpreter, causing my lint in the virtual environment not been used.
And the virtual env interpreter did not even show in "select python interpreter". But as written, opening the IDE at the level where the .vscode folder, that has the settings.json file with the correct path, it does.
Once you set the correct path in the setting.json file and select the virtual environment interpreter, then Visual Studio Code will automatically activate the virtual environment in its terminal:
Many have mentioned the python.pythonPath
method.
Another way is adding a envFile
in the launch.json
like this:
{
"name": "Run",
"etc": "etc",
"envFile": "${workspaceFolder}/venv"
}
I fixed the issue without changing the Python path as that did not seem like the right solution for me. The following solution worked for me, and hopefully it works for you as well :))
-
Open cmd in Windows / shell in Linux/Mac.
-
Activate your virtualenv (using source activate / activate.bat / activate.ps1 if using PowerShell)
C:Users<myUserName>VideosmyFolder>django-projectScriptsactivate.bat (django-project) C:Users<myUserName>VideosmyFolder>
-
Navigate to your project directory and open Visual Studio Code there.
(django-project) C:UsersprashVideosmyFolderprojects>code .
-
in Visual Studio Code, go to menu File → Preferences → Settings (don’t worry you don’t need to open the JSON file)
-
In the setting search bar, search for virtual / venv and hit Enter. You should find the below in the search bar:
Python: Venv Folders Folders in your home directory to look into for
virtual environments (supports pyenv, direnv and virtualenvwrapper by
default).
-
Add an item, and then enter the path of the scripts of your virtuanenv which has the activate file in it. For example, in my system, it is:
C:Users<myUserName>VideosmyFolderdjango-projectScripts
-
Save it and restart Visual Studio Code.
-
To restart, open cmd again, navigate to your project path and open Visual Studio Code.
(Note that your venv should be activated in cmd before you open Visual Studio Code from cmd)
Command to open Visual Studio Code from cmd:
code .
I had the same problem and the solution was pretty easy:
"If you create a new conda environment while VS Code is running, use
the Reload Window command to refresh the environment list shown with
Python: Select Interpreter; otherwise you may not see the environment
there. It might take a short time to appear; if you don’t see it at
first, wait 15 seconds then try using the command again."
That’s written on the Visual Studio Code site.
Note: to reload the window: Ctrl + Shift + P in Visual Studio Code, then write reload window
There is a Visual Studio Code extension called "Python Auto Venv" that automatically detects and uses your virtual environment if there is one.
With a newer Visual Studio Code version it’s quite simple.
Open Visual Studio Code in your project’s folder.
Then open Python Terminal (Ctrl + Shift + P: Python: Create Terminal)
In the terminal:
python -m venv venv
You’ll then see the following dialog:
Click Yes; and your venv is ready to go.
Open a new terminal within VSCode Ctrl + Shift + P and you’ll see that venv is getting picked up; e.g.: (venv) ...
You can now instal packages as usual, e.g., pip install sklearn
To keep track of what is installed: pip freeze > requirements.txt
For the older versions of VSCode you may also need to do the following:
Then Python: Select Interpreter
(via Ctrl + Shift + P)
And select the option (in my case towards the bottom)
Python 3.7 (venv)
./venv/Scripts/python.exe
If you see
Activate.ps1 is not digitally signed. You cannot run this script on the current system.
you’ll need to do the following: https://stackoverflow.com/a/18713789/2705777
For more information see: Global, virtual, and conda environments
Installing Modules
Ctrl + Shift + P and Terminal: Create New Integrated Terminal
from the terminal
Windows: ..venvScriptsactivate
Linux: ./.venv/bin/activate
You can now instal packages as usual, e.g., pip install sklearn
.
For Jupyter, you need to do more – Jupyter notebooks in Visual Studio Code does not use the active virtual environment
In Visual Studio Code, select a folder, create a workspace and it will work fine.
For Mac users, note this bug: when you click "Enter interpreter path", you have two options: (1) manually enter the path; (2) select the venv file from Finder.
It only works if I manually enter the path. Selecting with Finder yields some strange path like Library/Developer/CommandTools/...
which I understand.
-
If your using Visual Studio Code on Mac, it’s important to have your venv installed in the same directory as your workspace.
-
In my case my venv was in a different directory (not in my project workspace), so a simple cut/copy-paste of my venv to the project workspace did the trick.
-
As soon as your venv is copied to the project workspace, your Visual Studio Code will pick that up and show a notification giving you an option to select venv as an interpreter.
Steps to create virtual environment:
- go to folder containing project
python3 -m venv evn_name
source evn_name/bin/activate
- now you will be able to see (env_name) infront of the each terminal line
Now you can install required libraries in virtual environment
pip3 install -r requirement.txt
- if needed restart code editor
to stop working in virtual environment type: deactivate
to remove virtual environment type: rm -rf evn_name
Let’s assume that you have created a virtualenv folder with the name of venv.
You can easily activate it by typing the following command from the directory where venv is installed.
.venvScriptsactivate
If you already have your virtualenvs
, you only need to open VSCode preferences (Ctrl + ,)
and search for venv
. Then add the path of the virtualenvs
to the “Venv Path”
settings, like so:
More information can be found here:
https://techinscribed.com/python-virtual-environment-in-vscode/
Two main things I identified that could lead to being unable to see the venv.
- If you are using VS Code version 1.60.0 or closer to that, the path to the venv is given by
python.defaultInterpreterPath
. The python.pythonPath
is no longer valid.
So, the settings.json should be like:
{
"python.defaultInterpreterPath":
"C:\tproj\tproj_env\Scripts\python"
}
- The venv folder should be in a higher level folder than where the
settings.json
is in.
ex: if the project name is tproj and the venv folder (say tproj_env) and codes folder (say tproj_code) are sub-folders. It is recommended to have the settings.json
be in the tproj_code sub-folder.
VS Code python extension automatically checks for virtual environments in default virtual environment directories: https://code.visualstudio.com/docs/python/environments#_where-the-extension-looks-for-environments
If your virtual environment is present in any of these default directories, you just need to activate the environment by selecting it. To select a specific environment, use the ‘Python: Select Interpreter’ command from the Command Palette (Ctrl+Shift+P).
Refer: https://code.visualstudio.com/docs/python/environments#_select-and-activate-an-environment
Start PowerShell with admin privileges, run the following command:
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Remotesigned
Confirm and go! Execution Policy changes have been updated. You can go back to VsCode and activate your virtual env.
You have to select python that is in the virtual environment
- create new venv (virtualenv -p python3 venv)
- open directory (with venv) in Vs Code
- CMD + Shift + P: Python: Select Interpreter from venv
There is a very simple procedure for the new versions of vscode
- Open a command pallatte (ctrl+shift+p)
- Select "+Enter interpreter path"
- Select "Find.." and it will open the file explorer
- Go to your eviroiment folder’s script folder (ex:C:UsersnameenvScripts)
- Select the python.exe file
- Restart the VScode and if you see the available kernlas you can find your pyhton env there
With Visual Studio Code, a task within task.json
can be configured to build a python environment with python modules listed in the requirements.txt
and upgrade pip
within that virtual environment. To create the task, goto Terminal -> Configure Tasks. If there is no task.json
existing in your environment, create task.json
file from template -> then click other and copy and paste the json below. This task is agnostic to Operating Systems such as Windows, Linux, and MacOS. With Windows’ ecosystem of command-line applications, cmd.exe
is used in conjunction with the flags /c
and /d
to ensure that the environment created by the task is correct. As mentioned by Neil in his post, you can either select
Yes after the dialog prompt or select the python interpreter via the command palette.
{
"version": "2.0.0",
"tasks": [
{
"label": "Build Python Env",
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
},
"linux": {
"options": {
"cwd": "${workspaceFolder}"
},
"command": "python3 -m venv py_venv && source py_venv/bin/activate && python3 -m pip install --upgrade pip && python3 -m pip install -r requirements.txt && deactivate py_venv"
},
"osx": {
"options": {
"cwd": "${workspaceFolder}"
},
"command": "python3 -m venv py_venv && source py_venv/bin/activate && python3 -m pip install --upgrade pip && python3 -m pip install -r requirements.txt && deactivate py_venv"
},
"windows": {
"options": {
"shell": {
"executable": "C:\Windows\system32\cmd.exe",
"args": [
"/d",
"/c"
]
},
"cwd": "${workspaceFolder}"
},
"command": "(if not exist py_venv py -m venv py_venv) && .\py_venv\Scripts\activate.bat && py -m pip install --upgrade pip && py -m pip install -r requirements.txt && deactivate py_venv"
},
"problemMatcher": []
}
]
}
The task labeled Build Python Env is demonstrated to install a Python environment with numpy and pandas indicated as dependencies in requirements.txt
example below:
If more python packages are added to the requirements.txt
, then re-running the task will install the new additional dependencies to the python virtual environment
By setting EVERYTHING up in Visual Studio Code, your virtual environment will not have the configuration that you want.
First use GitBash and type these commands to create your virtual environment. This assumes you are in the correct folder where Python was installed.
python3 -m venv --system-site-packages /c/python/python39/python39_venv
cd /c/python/python39/python39_venv
source /c/python/python39/python39_venv/source/bin/activate
pip install {module of choice}=={version of choice}
If you are in a different folder, then type the full path of python.exe instead of the command python3.
/c/Python/Python39/Scripts/python.exe -m venv --system-site-packages /c/python/python39/python39_venv
Now configure this new virtual environment in Visual Studio Code:
View > Command Palette > type: "Python: Select Interpreter" > type "C:/Python/Python39/python39_venv/Scripts/python.exe"
Depending on the version of Python, the subfolders to python.exe might vary a bit. So manually navigate to the path to confirm the path.
Activate your virtual environment:
- Open the command prompt (Windows) or terminal (MacOS).
- Activate the virtual environment:
- replace venv with your virtual environment name
- assumes you use Anaconda or Miniconda as your package manager
conda activate venv
Open the project folder in Visual Studio (VS) Code:
- Launch VS Code.
- Click on "File" in the menu bar.
- Select "Open Folder" or "Open Workspace" (if you have a workspace file).
- Navigate to the folder that contains your project files and click "Open."
Configure the Python interpreter in VS Code:
- Once your project is open in VS Code, press Ctrl+Shift+P (Windows) or Cmd+Shift+P (MacOS) to open the Command Palette.
- Type "Python: Select Interpreter" and select it from the options.
- A list of available Python interpreters will appear. Choose the one associated with your virtual environment (it might be labeled with the environment name and path).
If your virtual environment is not listed, select "Enter interpreter path…" and provide the path to the Python executable within your virtual environment.
-
Create a virtual environment:
python3 -m venv my_env_name
-
Activate the virtual environment
source bin/activate
Type :python -c "import sys; print(sys.executable)"
you will get something like :
for linux : /home/user/dev/env/frame/bin/python
copy this.
-
Open VS Code
-
Press Ctrl+Shift+P
-
Search for Python Select Interpreter
-
Paste the file directory
Open a new terminal, you will see your virtual environment.
You can easily create a Python virtual environment in Visual Studio Code (VSCode) by following these steps:
-
Open the terminal in VSCode. You can do this by navigating to the menu and selecting View -> Command Palette, or by using the keyboard shortcut Ctrl+Shift+p.
-
-
Choose between conda or venv. In this example, we’ll use venv.
-
Select your desired Python version from the list of installed Python versions on your machine.
-
If you have a requirements.txt file that you want to use to install packages, you can specify it here. The virtual environment will be created based on the packages listed in this file. If not, you can leave this option blank.
-
Your Python virtual environment will now be created, and it will be located in a directory named .venv within your project folder.
This method allows you to easily manage your Python environments within VSCode and select the Python version and package dependencies that suit your project’s requirements.
For me I think the problem was that the name of my virtual environment wasn’t preceded by a .
and Visual Studio Code wasn’t recognising it as a Virtual Environment. (Running on MAC).
When creating one on the terminal with, for example:
python3.12 -m venv .hello
and opening the folder that contains .hello
, the Visual Studio Code will suggest .hello
as an Interpreter when pressing Cmd
+ Shift
+ P
and selecting Python: Select Interpreter
.
To see if it works, you can press the run button and open the terminal on VSCode. The path shown should be something like:
/Users/youruser/Desktop/FolderWithVenv/.hello/bin/python
In my project folder I created a venv folder:
python -m venv venv
When I run command select python interpreter
in Visual Studio Code, my venv folder is not shown. I went one level up like suggested here, but Visual Studio Code doesn’t see my virtual interpreter.
What did I miss?
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.defaultInterpreterPath": "Your_venv_path/bin/python"
under workspace settings.
(For Windows): Update"python.defaultInterpreterPath": "Your_venv_pathScriptspython.exe"
under workspace settings. -
Restart Visual Studio Code in case if it still doesn’t show your venv.
Note: Use python.pythonPath
instead of python.defaultInterpreterPath
for older versions.
I had the same problem and it was because PowerShell was not updated. Sometimes Windows preserves version 2.* and I had to manually download and install version 3.
After that, the problem was solved and I could use virtual environments very well.
I was having the same issue until I worked out that I was trying to make my project directory and the virtual environment one and the same – which isn’t correct.
I have a CodePython
directory where I store all my Python projects.
My Python 3 installation is on my Path.
If I want to create a new Python project (Project1) with its own virtual environment, then I do this:
python -m venv CodePythonProject1venv
Then, simply opening the folder (Project1) in Visual Studio Code ensures that the correct virtual environment is used.
This is an addition to Sumit S Chawla’s answer that, though it is correct, is missing the fact that anytime you open a folder in Visual Studio Code, it creates a .vscode folder, but those can be multiple, created any time you eventually open a directory.
The .vscode folder has JSON objects that content properties such "setting.json", in which one declare the interpreter to use at that the ".vscode" level (refer to What is a 'workspace' in Visual Studio Code? for more clarifications).
{
{
"python.pythonPath": "VirtualEnPath/bin/python3.6"
}
}
So potentially you could open Visual Studio Code at another level in the virtual environment. It creates another .vscode folder that assume as Python directory those of the global machine and so having such an error, and has I experienced has nothing to do if the virtual environment is activated or not.
This is indeed what happened to me. I have indeed a DjangoRESTAPI_GEN folder in which I initially opened the IDE and it did recognize the virtual environment Python path. Then a few days after I opened it at the level where Git is, so it did create another .vscode folder, that picked the global Python Interpreter, causing my lint in the virtual environment not been used.
And the virtual env interpreter did not even show in "select python interpreter". But as written, opening the IDE at the level where the .vscode folder, that has the settings.json file with the correct path, it does.
Once you set the correct path in the setting.json file and select the virtual environment interpreter, then Visual Studio Code will automatically activate the virtual environment in its terminal:
Many have mentioned the python.pythonPath
method.
Another way is adding a envFile
in the launch.json
like this:
{
"name": "Run",
"etc": "etc",
"envFile": "${workspaceFolder}/venv"
}
I fixed the issue without changing the Python path as that did not seem like the right solution for me. The following solution worked for me, and hopefully it works for you as well :))
-
Open cmd in Windows / shell in Linux/Mac.
-
Activate your virtualenv (using source activate / activate.bat / activate.ps1 if using PowerShell)
C:Users<myUserName>VideosmyFolder>django-projectScriptsactivate.bat (django-project) C:Users<myUserName>VideosmyFolder>
-
Navigate to your project directory and open Visual Studio Code there.
(django-project) C:UsersprashVideosmyFolderprojects>code .
-
in Visual Studio Code, go to menu File → Preferences → Settings (don’t worry you don’t need to open the JSON file)
-
In the setting search bar, search for virtual / venv and hit Enter. You should find the below in the search bar:
Python: Venv Folders Folders in your home directory to look into for
virtual environments (supports pyenv, direnv and virtualenvwrapper by
default).
-
Add an item, and then enter the path of the scripts of your virtuanenv which has the activate file in it. For example, in my system, it is:
C:Users<myUserName>VideosmyFolderdjango-projectScripts
-
Save it and restart Visual Studio Code.
-
To restart, open cmd again, navigate to your project path and open Visual Studio Code.
(Note that your venv should be activated in cmd before you open Visual Studio Code from cmd)
Command to open Visual Studio Code from cmd:
code .
I had the same problem and the solution was pretty easy:
"If you create a new conda environment while VS Code is running, use
the Reload Window command to refresh the environment list shown with
Python: Select Interpreter; otherwise you may not see the environment
there. It might take a short time to appear; if you don’t see it at
first, wait 15 seconds then try using the command again."
That’s written on the Visual Studio Code site.
Note: to reload the window: Ctrl + Shift + P in Visual Studio Code, then write reload window
There is a Visual Studio Code extension called "Python Auto Venv" that automatically detects and uses your virtual environment if there is one.
With a newer Visual Studio Code version it’s quite simple.
Open Visual Studio Code in your project’s folder.
Then open Python Terminal (Ctrl + Shift + P: Python: Create Terminal)
In the terminal:
python -m venv venv
You’ll then see the following dialog:
Click Yes; and your venv is ready to go.
Open a new terminal within VSCode Ctrl + Shift + P and you’ll see that venv is getting picked up; e.g.: (venv) ...
You can now instal packages as usual, e.g., pip install sklearn
To keep track of what is installed: pip freeze > requirements.txt
For the older versions of VSCode you may also need to do the following:
Then Python: Select Interpreter
(via Ctrl + Shift + P)
And select the option (in my case towards the bottom)
Python 3.7 (venv)
./venv/Scripts/python.exe
If you see
Activate.ps1 is not digitally signed. You cannot run this script on the current system.
you’ll need to do the following: https://stackoverflow.com/a/18713789/2705777
For more information see: Global, virtual, and conda environments
Installing Modules
Ctrl + Shift + P and Terminal: Create New Integrated Terminal
from the terminal
Windows: ..venvScriptsactivate
Linux: ./.venv/bin/activate
You can now instal packages as usual, e.g., pip install sklearn
.
For Jupyter, you need to do more – Jupyter notebooks in Visual Studio Code does not use the active virtual environment
In Visual Studio Code, select a folder, create a workspace and it will work fine.
For Mac users, note this bug: when you click "Enter interpreter path", you have two options: (1) manually enter the path; (2) select the venv file from Finder.
It only works if I manually enter the path. Selecting with Finder yields some strange path like Library/Developer/CommandTools/...
which I understand.
-
If your using Visual Studio Code on Mac, it’s important to have your venv installed in the same directory as your workspace.
-
In my case my venv was in a different directory (not in my project workspace), so a simple cut/copy-paste of my venv to the project workspace did the trick.
-
As soon as your venv is copied to the project workspace, your Visual Studio Code will pick that up and show a notification giving you an option to select venv as an interpreter.
Steps to create virtual environment:
- go to folder containing project
python3 -m venv evn_name
source evn_name/bin/activate
- now you will be able to see (env_name) infront of the each terminal line
Now you can install required libraries in virtual environment
pip3 install -r requirement.txt
- if needed restart code editor
to stop working in virtual environment type: deactivate
to remove virtual environment type: rm -rf evn_name
Let’s assume that you have created a virtualenv folder with the name of venv.
You can easily activate it by typing the following command from the directory where venv is installed.
.venvScriptsactivate
If you already have your virtualenvs
, you only need to open VSCode preferences (Ctrl + ,)
and search for venv
. Then add the path of the virtualenvs
to the “Venv Path”
settings, like so:
More information can be found here:
https://techinscribed.com/python-virtual-environment-in-vscode/
Two main things I identified that could lead to being unable to see the venv.
- If you are using VS Code version 1.60.0 or closer to that, the path to the venv is given by
python.defaultInterpreterPath
. Thepython.pythonPath
is no longer valid.
So, the settings.json should be like:
{
"python.defaultInterpreterPath":
"C:\tproj\tproj_env\Scripts\python"
}
- The venv folder should be in a higher level folder than where the
settings.json
is in.
ex: if the project name is tproj and the venv folder (say tproj_env) and codes folder (say tproj_code) are sub-folders. It is recommended to have thesettings.json
be in the tproj_code sub-folder.
VS Code python extension automatically checks for virtual environments in default virtual environment directories: https://code.visualstudio.com/docs/python/environments#_where-the-extension-looks-for-environments
If your virtual environment is present in any of these default directories, you just need to activate the environment by selecting it. To select a specific environment, use the ‘Python: Select Interpreter’ command from the Command Palette (Ctrl+Shift+P).
Refer: https://code.visualstudio.com/docs/python/environments#_select-and-activate-an-environment
Start PowerShell with admin privileges, run the following command:
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Remotesigned
Confirm and go! Execution Policy changes have been updated. You can go back to VsCode and activate your virtual env.
You have to select python that is in the virtual environment
- create new venv (virtualenv -p python3 venv)
- open directory (with venv) in Vs Code
- CMD + Shift + P: Python: Select Interpreter from venv
There is a very simple procedure for the new versions of vscode
- Open a command pallatte (ctrl+shift+p)
- Select "+Enter interpreter path"
- Select "Find.." and it will open the file explorer
- Go to your eviroiment folder’s script folder (ex:C:UsersnameenvScripts)
- Select the python.exe file
- Restart the VScode and if you see the available kernlas you can find your pyhton env there
With Visual Studio Code, a task within task.json
can be configured to build a python environment with python modules listed in the requirements.txt
and upgrade pip
within that virtual environment. To create the task, goto Terminal -> Configure Tasks. If there is no task.json
existing in your environment, create task.json
file from template -> then click other and copy and paste the json below. This task is agnostic to Operating Systems such as Windows, Linux, and MacOS. With Windows’ ecosystem of command-line applications, cmd.exe
is used in conjunction with the flags /c
and /d
to ensure that the environment created by the task is correct. As mentioned by Neil in his post, you can either select
Yes after the dialog prompt or select the python interpreter via the command palette.
{
"version": "2.0.0",
"tasks": [
{
"label": "Build Python Env",
"type": "shell",
"group": {
"kind": "build",
"isDefault": true
},
"linux": {
"options": {
"cwd": "${workspaceFolder}"
},
"command": "python3 -m venv py_venv && source py_venv/bin/activate && python3 -m pip install --upgrade pip && python3 -m pip install -r requirements.txt && deactivate py_venv"
},
"osx": {
"options": {
"cwd": "${workspaceFolder}"
},
"command": "python3 -m venv py_venv && source py_venv/bin/activate && python3 -m pip install --upgrade pip && python3 -m pip install -r requirements.txt && deactivate py_venv"
},
"windows": {
"options": {
"shell": {
"executable": "C:\Windows\system32\cmd.exe",
"args": [
"/d",
"/c"
]
},
"cwd": "${workspaceFolder}"
},
"command": "(if not exist py_venv py -m venv py_venv) && .\py_venv\Scripts\activate.bat && py -m pip install --upgrade pip && py -m pip install -r requirements.txt && deactivate py_venv"
},
"problemMatcher": []
}
]
}
The task labeled Build Python Env is demonstrated to install a Python environment with numpy and pandas indicated as dependencies in requirements.txt
example below:
If more python packages are added to the requirements.txt
, then re-running the task will install the new additional dependencies to the python virtual environment
By setting EVERYTHING up in Visual Studio Code, your virtual environment will not have the configuration that you want.
First use GitBash and type these commands to create your virtual environment. This assumes you are in the correct folder where Python was installed.
python3 -m venv --system-site-packages /c/python/python39/python39_venv
cd /c/python/python39/python39_venv
source /c/python/python39/python39_venv/source/bin/activate
pip install {module of choice}=={version of choice}
If you are in a different folder, then type the full path of python.exe instead of the command python3.
/c/Python/Python39/Scripts/python.exe -m venv --system-site-packages /c/python/python39/python39_venv
Now configure this new virtual environment in Visual Studio Code:
View > Command Palette > type: "Python: Select Interpreter" > type "C:/Python/Python39/python39_venv/Scripts/python.exe"
Depending on the version of Python, the subfolders to python.exe might vary a bit. So manually navigate to the path to confirm the path.
Activate your virtual environment:
- Open the command prompt (Windows) or terminal (MacOS).
- Activate the virtual environment:
- replace venv with your virtual environment name
- assumes you use Anaconda or Miniconda as your package manager
conda activate venv
Open the project folder in Visual Studio (VS) Code:
- Launch VS Code.
- Click on "File" in the menu bar.
- Select "Open Folder" or "Open Workspace" (if you have a workspace file).
- Navigate to the folder that contains your project files and click "Open."
Configure the Python interpreter in VS Code:
- Once your project is open in VS Code, press Ctrl+Shift+P (Windows) or Cmd+Shift+P (MacOS) to open the Command Palette.
- Type "Python: Select Interpreter" and select it from the options.
- A list of available Python interpreters will appear. Choose the one associated with your virtual environment (it might be labeled with the environment name and path).
If your virtual environment is not listed, select "Enter interpreter path…" and provide the path to the Python executable within your virtual environment.
-
Create a virtual environment:
python3 -m venv my_env_name
-
Activate the virtual environment
source bin/activate
Type :python -c "import sys; print(sys.executable)"
you will get something like :
for linux :/home/user/dev/env/frame/bin/python
copy this. -
Open VS Code
-
Press
Ctrl+Shift+P
-
Search for
Python Select Interpreter
-
Paste the file directory
Open a new terminal, you will see your virtual environment.
You can easily create a Python virtual environment in Visual Studio Code (VSCode) by following these steps:
-
Open the terminal in VSCode. You can do this by navigating to the menu and selecting View -> Command Palette, or by using the keyboard shortcut Ctrl+Shift+p.
-
Choose between conda or venv. In this example, we’ll use venv.
-
Select your desired Python version from the list of installed Python versions on your machine.
-
If you have a requirements.txt file that you want to use to install packages, you can specify it here. The virtual environment will be created based on the packages listed in this file. If not, you can leave this option blank.
Your Python virtual environment will now be created, and it will be located in a directory named .venv within your project folder.
This method allows you to easily manage your Python environments within VSCode and select the Python version and package dependencies that suit your project’s requirements.
For me I think the problem was that the name of my virtual environment wasn’t preceded by a .
and Visual Studio Code wasn’t recognising it as a Virtual Environment. (Running on MAC).
When creating one on the terminal with, for example:
python3.12 -m venv .hello
and opening the folder that contains .hello
, the Visual Studio Code will suggest .hello
as an Interpreter when pressing Cmd
+ Shift
+ P
and selecting Python: Select Interpreter
.
To see if it works, you can press the run button and open the terminal on VSCode. The path shown should be something like:
/Users/youruser/Desktop/FolderWithVenv/.hello/bin/python