Post

Python Virtual Environments On Ubuntu

virtualenv is a tool to create isolated Python environments. virtualenv creates a folder which contains all the necessary executables to use the packages that a Python project would need.

Example: In order to keep your apps independent, each will run in its own virtual environment. Create an environment for each application using the virtualenv command. In each environment, activate and install its dependencies. Follow the steps described 

in this article

How do I install virtualenv on my linux/ubuntu

You can verify that Python 3 is installed on your system by running:

python3 --version

The output should look like this:

OUTPUT
Python 3.6.7

Let’s start by installing the virtualenv. run the following command to install virtualenv

sudo apt-get install python-virtualenv

Once the installation is complete, we are ready to create our virtual environment for python3 and python2

How do I create virtualenv in Python3 

First, open your terminal and navigate to your project directory e.g cd myVirtualEnv. Once you are done

run the following command to create your virtual env

virtualenv -p python3 envname

Ensure that there is an internet connection before running the command. Once done, Our virtualenv is ready.

You can now activate your virtual Environment with the following command:

source envname/bin/activate

The Output should look like this

OUTPUT
(envname)ubuntu@ubuntu:~$ 

Upgrade virtualenv with the following command. NB: Do not use sudo when your virtual environment is activated else it will install in your entire machine, not on your virtualenv

Upgrade virtualenv:

pip install --upgrade virtualenv

  From here you can install your dependencies and start running your virtual environment. For any doubts or clarification, comment below and we will be ready to help.

How do I create virtualenv in Python2

Let’s start by installing the virtualenv. run the following command to install virtualenv

sudo apt-get install python-virtualenv

Once the installation is complete, we are ready to create our virtual environment for python2

First, open your terminal and navigate to your project directory e.g cd myVirtualEnv. Once you are done

run the following command to create your virtual env

virtualenv myVirtualEnv/envname --no-site-packages

Or this command if you are already in the myVirtualEnv directory

Running virtualenv with the option --no-site-packages will not include the packages that are installed globally. This can be useful for keeping the package list clean in case it needs to be accessed later.

virtualenv envname --no-site-packages

Ensure that there is an internet connection before running the command. Once done, Our virtualenv is ready.

You can now activate your virtual Environment with the following command:

source envname/bin/activate

The Output should look like this

OUTPUT
(envname)ubuntu@ubuntu:~$ 

Upgrade virtualenv with the following command. NB: Do not use sudo when your virtual environment is activated else all dependencies will install in your entire machine, not on your virtualenv

Upgrade virtualenv:

pip install --upgrade virtualenv

 

 From here you can install your dependencies and start running your virtual environment.

If you are facing any problem, feel free to leave a comment. We are always ready to help.

Did this help you?

  
Not clear with something? drop a Comment
author

  • related posts

    0 comments

    leave a reply