Python - Correct Usage of Virtual Environments & Setup via VsCode Terminal

asrın öztin
3 min readSep 21, 2021

Virtual Environment is a tool that is developed in order to keep dependencies required by different projects separate by creating isolated environments.

Why Do We Need Virtual Environments ?

As Python or the site-packages used in the project are updated, incompatibility issues arise and it is, therefore, necessary to create a separate virtual environment for each project.

Otherwise, the site-packages that are installed and used in a project created today, may be incompatible with the packages that have not received updates, and also with the program itself, since python or packages may have been received updates in the future.

When a separate virtual environment is used, the libraries in the isolated environments do not interact with each other, so the library versions used during the project remain stable and continue to work harmoniously unless they are intervened and updated in especial afterward.

How to Create a New Virtual Environment With Vscode - Terminal ?

As explained, it is a step that should be done at the very beginning of every project.

The package required for creating a virtual environment is “virtualenv”, and if it is not installed before, it can be installed as follows.

Type “pip install virtualenv”

If it has already been installed before, You will get an output like the one shown below, saying “requirement already satisfied”.

My personal recommendation for specifying the name of the virtual environment is to start with “venv” and continue with the version of python to be installed (such as “python_3.9.5"). Thus, even after a long time has passed since your project, you can learn about the python version used just by looking at the folder name.

Type “virtualenv name_of_your_virtual_environment”

As the last step, the environment must be activated.

Type “name_of_your_virtual_environment/Scripts/activate”

Setting the Environment as the Interpreter of the Project

You will probably be getting getting a pop-up message on the right bottom of the screen like the one below since you have created the project. It is because, even if you have created and activated the environment, it is not connected yet.

To connect the environment and select it as the interpreter, first open the Command Palette (Ctrl+Shift+P), and follow the steps shown below.

For detailed information, you can check VsCode’s documents/Environments

--

--

asrın öztin

Computer Science Engineering educated, with interest in Data Science.