How to set up a virtual Python development environment with venv

Below are the steps for creating a virtual Python development environment. If you don’t know what they means then below there is each step explained.

  • Update the system – sudo apt-get update
  • Upgrade the system – sudo apt-get -y upgrade
  • Check Python version – python for Python 2.X, or python3 for Python 3.X
  • Install pipsudo apt-get install -y python3-pip for Python 3.X
  • Install a few extra packages – sudo apt-get install build-essential libssl-dev libffi-dev python-dev
  • Install venvsudo apt-get install -y python3-venv
  • Create a folder for the environments – mkdir PythonProjects
  • Change directory to PythonProjectscd PythonProjects
  • Create the virtual environment – python3 -m venv MyWebsite
  • Activate/Deactivate the virtual environment – source MyWebsite/bin/activate or deactivate for deactivating it.

I use Linux on my machine but the steps shown below are the same for Windows and Mac.

About virtual environment

A virtual Python development environment is an isolated Python environment where you can install packages, libraries, etc without affecting the rest of the machine. It is very important to create a private Python development environment for every Python project because each project within the programming environment will have its own dependencies and won’t interact with other projects.

Updating the system

Want you want to do before everything else is to update the system. You can do that by running the following command in the terminal:

sudo apt-get update

Once you are done you should run the following command in the terminal:

sudo apt-get -y upgrade

The -y flag says that we agree to upgrade all the items.

Checking the version of Python you have installed on your system

The next thing you want to do is to make sure you have Python 3.X installed on your machine. Linux (at least Ubuntu 16.04) comes with Python 2.X and 3.X installed by default. You can do that by running the following commands:

ddn_ro@Linux:~$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
ddn_ro@Linux:~$ python3
Python 3.5.2 (default, Sep 14 2017, 22:51:06) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.

As you can see, I have both Python 2.X and Python 3.X installed.

Installing pip the Python’s package manager

Next, we have to install pip which is the package manager for Python. I want to use Python 3.X so I am going to install pip for Python 3.X.

sudo apt-get install -y python3-pip

Later on we will use pip to install packages we need for our projects.

Installing a few extra packages

There are four more packages and tools that are essentials for a good programming work-flow. They are:

  • build-essential
  • libssl-dev
  • libffi-dev
  • python-dev
    • Let’s install them buy running the following command:

      sudo apt-get install build-essential libssl-dev libffi-dev python-dev

      Installing venv

      Now, we are ready to set up the virtual programming environment. In order to do that we have to install venv module which is part of the standard Python 3 library. We will use venv to create the virtual environment.

      We install venv by running the following command in the terminal:

      sudo apt-get install -y python3-venv

      Creating a folder for the environments

      I want to be organized so I am going to create a folder named PythonProjects which will host all of my Python virtual environments. See it as a wrapper! I do that by running the following command in the terminal:

      mkdir PythonProjects

      Since I am in the home folder, the folder PythonProjects will be created there.

      mkdir command

      Creating the virtual environment

      Now, we are going to create the the first virtual environment using the venv we have just installed. We need to switch first into the PythonProjects folder.

      cd PythonProjects

      The command cd will change the directory from home to the newly created one, PythonProjects.

      Now, we are ready to create the virtual environment by running the following command into the terminal:

      python3 -m venv MyWebsite

      The command, python3 -m venv MyWebsite will create a new directory which will contain a few items. Those items work together to ensure that your project is isolated from the rest of your machine.

      virtual development environment

      Activate the virtual environment

      The environment we have just created has to be activated. We will activate it by running the following command in the terminal:

      source MyWebsite/bin/activate

      Now, the environment is activated and the terminal prompt should be prefixed like is shown below:

      (MyWebsite) ddn_ro@Linux:~/PythonProjects$

      That means our environment is active now and when we create programs here they will use only this environment settings and packages. When you want to leave the environment you just type the command deactivate.

Leave a Reply