Bitz'nPieces

Home Tech Notes Blog Art Gallery About

Python's virtual environments for installing packages

The first step towards setting up a new project is to prepare the environment by installing all the dependencies that will be used in the project. It is best to isolate your working environment from your system's base environment, so that just in case something goes wrong it can be easily reverted without affecting your OS or base programs. Python has a very useful package to handle just this, called virtualenv. This tutorial will walk you through installing virtualenv and setting up your own virutal environment.

So let's get started with the installations, first we upgrade pip and then install the virtualenv package like this:

pip install --user --upgrade pip pip install --user virtualenv

Next go ahead and create a virtual environment for you to work in. I named the virutal environment as "venv"

virtualenv venv

From now on any changes or new installations you want to make for your project should be in this enviroment. To activate the virtual environment open a terminal and cd to the project's folder/workspace and run the following command:

source venv/bin/activate

Voila! you have created a dedicated environment to tinker around with your project and its dependencies in peace!

One last thing, just to be sure if you are working in the virtual-environment or in the base-environment a post script is added to the terminal prompt indicating that you are now in the virtual-environment like so:

(venv) (base) user-name:~/proj-workspace$

The (venv) at the start of the path specifier indicates that in the current terminal virtual environment is active and any and all changes you make will be in this dedicated environment.

Thats it for now, as always let me know if I missed something or if you found a mistake. Happy coding!

* ------ * ------ * ------ *