Emmet Friel cyber security blog

Python Virtual Environment in VSCode

Why use a virtual environment?

Its good practice to isolate dependencies and libraries between applications whatever language your coding in so everything isn’t lumped into your system wide dependency list. It makes installing, maintaining and version controlling project dependencies alot easier and porting the project over to other people to use means that their not installing unncessary dependencies. Visual Studio Code IDE provides a nice feature for creating and working with Python environments. By default, the Python vscode extension looks for and uses the first Python interpreter it finds in the system path. If it doesn’t find an interpreter, it issues a warning. We can change this default behaviour to point VSCode to a Python interpreter that will activate a virtual environment.


Dependencies

sudo apt-get install python3-pip python3-venv
pip3 install virtualenv


Create project structure

mkdir project

Inside projects folder create two additional directories:

cd project
mkdir src/
python -m venv virtual_env/

Navigate into src/ directory, create an empty python file and open up vscode:

cd src/
touch test.py
code .


Add python virtual environment to vscode

CTRL + shift + p
Preferences: Open Workspace Settings (JSON)

Insert the full path to your python virtual environment folder in settings.json:

{ 
    "python.pythonPath": "/home/user/Documents/project/virtual_env" 
}

Save and exit settings.json.
If you have terminals open on vscode then close them all and open a new terminal. You should see vscode automatically recognising your python virtual environment in the terminal as seen below:

image

From here you can install and maintain your python packages which are stored on your virtual environment:

virtual_env/lib/python3.8/site-packages


Important notes

If it doesn’t work first time around these work arounds I found that conflicted with the python virtual environment that needed removed: