Setting up Python Virtual Development Environment in Visual Studio Code
1. Python Installation
- Download latest python installer and install it.
https://www.python.org/ftp/python/3.9.0/python-3.9.0-amd64.exe
- During installation deselect
Install launcher for all users
option to install it only for current user.
- Deselect
Add Python X.X to PATH
option. We will do it manually.
- Click on
Customize installation
- Click
Next
- Select
Precompile standard library
Customize install location
to your preferred path. In this case it is E:\Tools\Python39
2. Create Virtual Environment
- Start windows command prompt
- Update pip using following command:
E:\Tools\Python39\python.exe -m pip install --upgrade pip
- Create virtual environemnt using following command:
E:\Tools\Python39\python.exe -m venv E:\PythonVirtualEnvironments\py39env
- Activate the virtual environment and check python version, path
E:\PythonVirtualEnvironments\py39env\Scripts\activate
- Upgrade pip in virtual environment:
python -m pip install --upgrade pip
- The virtual environment setup is done. You can create seperate virtual enviroments for different projects and install project specific python libraries in the project specific virtual environment instead of installing python libraries system wide.
- Use following command in the activated virtual environment to install specific python libraries:
python -m pip install <library_1> <library_2> ...
- To list the installed libraries use following command:
pip list
- To exit the virtual environment use following command:
deactivate
3. Addin python to PATH environment variable for current user account
- Start
Edit environment variables for your account
- Double Click on
Path
Variable
- Add following entry:
E:\Tools\Python39
4. Visual Studio Code and Python Extension Installation
- Download and Install
Visual Studio Code
- Start
Visual Studio Code
and install Python extension for Visual Studio Code
published by Microsoft
5. Setting Up Project to use virtual environment
- Create a folder for your project files. Example:
PythonApp
- Add
app.py
file to project folder with simple print statement.
print("Hello Python")
- Open Project folder in
Visual Studio Code
- Open the
app.py
file in editor
- From the bottom left of status bar select any one python interpreter. This will create a folder
.vscode
in your project folder for VSCode specific settings. It will also create a settings.json
file
- Edit / Add following lines in
settings.json
file as per your python and virtual environment location
"python.venvPath":"E:\\PythonVirtualEnvironments",
"python.pythonPath":"E:\\PythonVirtualEnvironments\\py39env\\Scripts\\python.exe"
- Restart VSCode and open project and open
app.py
- Now you can run
app.py
from VSCode IDE. It will automatically activate the virtual environment to execute the python code.
6. Exporting and importing project dependencies
- Export environment dependencies to file:
pip freeze > requirements.txt
- Import environment dependencies from file:
pip install -r requirements.txt