Setting Python Virtual Environment

Bright
3 min readMar 19, 2022

--

Python icon

Python is a powerful and versatile programming language that’s used across several domains in Software Engineering. It’s very popular and can be used to develop a host of applications; it’s the language of choice when it comes to Data Science, ML, and AI. What makes Python shine is its beginner-friendly, elegant syntax.

Why use Virtual Environment

Virtual environments are somewhat like virtual machines. They create isolated environments to run specific python versions. This way, you can have multiple projects running different versions of python without hassle and it’s highly recommended to start any project in its own virtual environment. This helps insulate your projects from breaking changes as a result of major library upgrades, and also from switching between Python versions. It makes it possible for different python versions to coexist on a single machine.

Provisioning virtual environments creates a config directory with all necessary tools to manage the environment. If you’ve used nvm to manage your Node.js applications before, it’s essentially the same concept, albeit a few subtle differences. Don’t panic, there’s not much to do other than a simple CLI command to set everything up. We’ll run the command shortly but before then, let’s make sure we have python installed already.

If you’re on a Linux or Mac machine, chances are you already have it installed. If you don’t, visit the python official page; Windows users can install here.

Set up

Let’s first check the Python version we’re running and its path. You may have a slightly different version from what I have. NB: I’ll be using python3 on Ubuntu in this blog.

$ python3 -V
Python 3.6.9
$ which python3
/usr/bin/python3

Let’s install our virtual environment by running the following:

$ sudo apt update
$ sudo apt-get install python3-venv

What the above code does is update all our packages and install the virtual environment so we can get the latest version. If you’re a superuser, you don’t have to include the preceding sudo command.

Now, let’s create our project directory and cd into it. This can be done with a single command. Note that the directory name can be any arbitrary name.

$ mkdir python-sandbox && cd python-sandbox

We can go ahead and create out virtual environment with the following command:

$ python3 -m venv env
$ ls
env

After listing the contents of our project directory, it can be seen that we have a new env directory.

We have to source our virtual environment to activate the environment variables.

$ source env/bin/activate
(env)$

Once activated, the shell prompt is preceded by our environment variable name enclosed in parenthesis like so (env). It also comes pre-configured with pip (preferred installer program), a package manager like Java’s maven or Node’s npm to manage our packages (or libraries) so there’s not much heavy-lifting to do here. Isn’t it sweet?

All we have to do now it to upgrade our pip. Since python3 has been provisioned for us, we can use py other than the usual python command to save us a few keystrokes. Let’s run the following:

(env)$ py -m pip install --upgrade pip

We can check our installed packages by running the following:

(env)$ pip freeze
pkg-resources==0.0.0

To deactivate the virtual environment, we need to run the deactivate command like so:

(env)$ deactivate
$

The prompt will default to its pre-configured form, and the python configuration will revert to global.

Hurray!!! We’re all set now. Happy coding…

If you enjoyed this article, please follow my handle Bright for more stories about Software Development best practices and tech in general.

--

--