How to Create a Python Virtual Environment

Posted on

Introduction

In this article, the focus of the content is about how to create a python virtual environment. Python is one of most other modern programming languages nowadays. It has its own style and also an unique way for managing packages or modules. Managing those packages or modules such as downloading, storing, and resolving it. In a programming language aspect, it is one of the advantages for managing packages and modules in the most effective way. Because without any further consideration in the management of packages and modules, it can lead into some problems.

In Python programming language, there are actually a few different locations for those installed packages in the system. For example, usually most system packages are available in a specific place or location after the installation process. Basically, the main purpose of Python virtual environments is to create an isolated environment for Python projects. In other words, every specific project will have its own dependencies, regardless of what dependencies each project has. So, every project will have  a separate virtual environment each project.

Another advantage for having a virtual environment is that there are no limits to the number of the virtual environments.  They can have as many as they can because they are just directories containing a few scripts. Furthermore, it is easy to create them.

 

Steps for creating Python Virtual Environment

Below is how to create a python virtual environment :

1. First of all, check if the virtualenv package is available or not in the operating system. The manual page informs that it is useful for creating virtual python instances. Just execute the following command to check if the virtualenv package exist or not :

root@hostname:~# apt list --installed | grep virtualenv
WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

python3-virtualenv/bionic,now 15.1.0+ds-1.1 all [installed,automatic]
virtualenv/bionic,now 15.1.0+ds-1.1 all [installed]
root@hostname:~# apt list --installed | grep virtualenv

2. As in the above output display, there are two packages with the name of ‘virtualenv’. For python 3, the package name is python3-virtualenv. If the package is not exist, execute the following command to install the suitable virtualenv package. It depends on the installed python on the system. The following example is the installation of virtualenv for the old version of python :

apt-get -y install virtualenv

3. After successfully installing ‘virtualenv’ package, just execute the following command to make sure that virtualenv exist. It is also for checking the version of the ‘virtualenv’ :

user@hostname:~/python$ virtualenv --version
16.6.0
user@hostname:~/python$ 

4. Finally, after checking the availability of ‘virtualenv’ tool, create a new python virtual environment by executing the following command :

user@hostname:~/python$ virtualenv --python=/usr/bin/python /home/user/python/python-env
Already using interpreter /usr/bin/python
New python executable in /home/user/python/python-env/bin/python
Installing setuptools, pip, wheel...
done.
user@hostname:~/python$ 

3 thoughts on “How to Create a Python Virtual Environment

Leave a Reply