Using Virtual Environments in Python

With the Positron IDE and on the Command Line

Author
Affiliation

Michael Großbach

Published

2025-06-19

Modified

2025-06-19

Abstract

Virtual environments are a good way to keep Python packages for different projects separate so as to not break stuff when one project needs a certain—maybe outdated—version of a package to work, while another one needs a different one.

1 Prerequisits

On Debian, install the python package python3-venv:

sudo apt install python3-venv

I use a default virtual environment (venv) for development, but project-specific ones (especially) for alien projects that sometimes come with specific version requirements. In the following I describe how to create virtual Python environments both in the terminal as well as from within Positron, the new, language agnostic code editor/IDE from posit, the people behind RStudio.

A default virtual environment is probably the thing you want to have as a beginner—you only need to set it up once and use it for all your projects, while in more complex situations you might be better off with project-specific ones. Additionally, virtual environments can either be set up within the actual project folder or outside of it. We discuss all combinations of the above and look at pros and cons.

2 A Default Virtual Environment

A default venv is used for many or all of ones Python projects. An environment hosts all the packages that are needed for the projects that use this environment. This implies that there is no limit to the number of possible environments on your computer as there is no limit to the number of projects either.

2.1 CLI

On the command line (CLI, for command line interface), setting up a virtual environment is quick and easy. In the terminal, type:

mkdir -p $HOME/.venvs

to create a folder for virtual environments, followed by:

python3 -m venv $HOME/.venvs/Default

to actually create the venv. Don’t forget to include the directory ~/.venvs/ in your /etc/rsnapshot.exclude (or whatever the exclude file for your backup solution is called).

Use the following to install python packages into your virtual env:

~/.venvs/Default/bin/python -m pip install <package-name>

or

~/.venvs/Default/bin/python -m pip install --upgrade <package-name>

to upgrade a package. To switch to the new virtual environment, type

source ~/.venvs/Default/bin/activate

2.2 Positron

The following generates a venv named ‘Default’ in the folder ~/.venvs/ using the terminal:

mkdir ~/.venvs python -m venv ~/.venvs/Default

Then in Positron, press Shift-Ctrl-P and type python select interpreter

and select Enter interpreter path… and type in the path to the new virtual environment—here: ~/.venvs/Default. Done.

For every new project you want to use your default venv with, you need to repeat the last bit in Positron once.

3 Project-specific Environments

Project-specific environments can be either located within the project folder, or outside of it somewhere else on the harddrive.

3.1 Inside of Project Folder

3.1.1 CLI

First, create the folder, where you want your project to reside, for example:

mkdir ~/my-new-project

Second, create a virtual environment within that folder; I often hide this virtual environment by giving it a dot-name:

python -m venv ~/my-new-project/.venv

But you can of course choose to have the environment sit in a normal, ‘visible’ folder without the leading dot in its name instead:

python -m venv ~/my-new-project/venv

To activate your project’s environment, just run

source ~/my-new-project/.venv/bin/activate

or

source ~/my-new-project/venv/bin/activate

3.1.2 Positron

This generates a virtual environment within the project folder:

  • open Positron
  • press Shift-Ctrl-P -> python create environment
  • run python create environment
  • select the Venv option
  • select the newly created venv as interpreter

Whenever you open that project folder in Positron again, the virtual environment just created will be used as the interpreter automatically.

3.2 Outside of Project Folder

3.2.1 CLI

The following generates a venv named ‘my-project’ in the folder ~/pyenvs/ using the terminal:

mkdir ~/pyenvs python -m venv ~/pyenvs/my-project

Don’t forget to activate your venv before actually using it!

3.3 Positron

To be able to use a venv outside of the project directory in Positron, you need to first issue the following commands in a terminal (you can of course use Positron’s own terminal if you wisch):

mkdir ~/pyenvs python -m venv ~/pyenvs/my-project

Then, in Positron, press Shift-Ctrl-P and type

python select interpreter

and select Enter interpreter path… and type in the path to the new virtual environment—here: ~/pyenvs/my-project. Done.

4 Discussion

The advantage of having a default virtual environment is that a lot of packages only need to sit on the computer’s hard drive once.

The disadvantage of a default virtual environment is that some projects need specific versions of certain packages which might in turn break other projects. For example, when a project A fails to work and one installs the required (possibly older) version of a given package, this conflict is not noticed until later when the other project B is used (run) again. The installation of the package’s version appropriate for this package B then breaks project A, and so on.

A trade-off would possibly be to have a default for most projects, and dedicated environments for projects requiring odd versions. This would possibly mean that one starts development within the default environment, and, once the requirement of a package’s odd version is noticed, moves the entire project to a new, dedicated environment.

4.1 Pro default env

  • Easy to create and use with and automatically detected by Positron
  • Virtual environment is automatically used when the project folder is opened in Positron
  • The virtual environment gets backed up with the project, so code from the backup always comes with the appropriate code

4.2 Con default env

  • The virtual environment gets backed up with the project, thus cluttering the backup with python packages that really do not belong there
  • Positron-generated virtual environments cannot be located at some other, central place, like e.g., ~/pyenvs/