How to use python venv

Gustav Willig
2 min readApr 3, 2021

--

In this article I would like to tell you a bit about the pyvenv command and the venv module that powers it. Also how o create self-contained Python environments in order to practice safe development and manage package dependency conflicts.

Photo by Paul Melki on Unsplash

But before I show you have to use python -m venv , I would like to explain why you should always use a virutal environement.

Top 3 reasons why to use Virtual Environments

  • For each project you will need to intall packages which aren’t in the Python standard Library
  • Sometimes you need even to install different versions of the same library for different projects
  • Having only the packages which are really neccessary for the project in requirement.txt. This eliminates all chances for possibly experience gross global installation and package collision errors.

How to use it

If you are using Python 3, then you should already have the venv module from the standard library installed.

If you are using Python2.7, I highly recommendy you to migrate to Python3.X. As of January 1st, 2020 no new bug reports, fixes, or changes will be made to Python 2, and Python 2 is no longer supported. A few changes were made between when we released Python 2.7.

To create a virtual environment, go to your project’s directory and run:

To create a virtual environment, go to your project’s directory and run:

  • Windows: python -m venv venv
  • Ubuntu: python3 -m venv venv

The second argument is the location to create the virtual environment. Normally you willl call it env. You should keep in mind to add this directory to your .gitginore file. Every venv taking up about 25MB.

Activating a venv

Before you can start using your venv, you’ll need to activate it. Activating a venv will put the virtual environment-specific python and pip executables into your shell’s PATH.

  • Windows: venv\Scripts\activate
  • Ubuntu: source venv/bin/activate

--

--

Gustav Willig
Gustav Willig

Written by Gustav Willig

An AI Full-Stack Developer with a passion for using data to drive business decisions. Get your latest news about Django and AI trends by subscribing

No responses yet