[Django Tutorial] How to Activate Python Virtualenv / Virtual Environment in Linux?

Why do we need a Virtual Environment?

Developing a program/software is all about creating product with some programming language which solves our day to day problems. In order to write programs we need programming language first, but the libraries even prior to the language. Because, let’s say you want to write a code which displays current date and time in python. Than first of all you will need a module which has codes that interacts with the OS and hardware to fetch the system time and date.

But your program doesn’t only have a single module. We have different modules to be included. Also we build code in our local platform i.e. Linux/Windows/Mac OS. But in order to make our program run in all three environment, instead of building the code in particular environment what we can do is create a virtual environment.

So once we create a virtual environment, we have following feasibility:

  1. Portability of Code and associated modules and interpreter to convert our code to binary
  2. Program is accessible and executable in all environment.

Install VirtualEnv in Linux

Now our program is independent of the operating system and runs over every environment. For that we now need to install Virtualenv from our terminal. So in terminal (Ctrl + Alt + T) type the following command:

sudo apt install virtualenv

Upon successful execution of this command you now have your virtual environment setup in your desired directory. You will see certain files installed in that directory. Better do this on an empty directory to see what files and folders were actually added.

Activate your Virtual Environment

After successful installation of Virtualenv, it is now time to create a virtual environment for your python interpreter. Since I am running python3 on my machine, use the following code to create your virtual environment

virtualenv -p python3 .

Once you have created your virtual environment in the current folder which is pointed by the dot ( . ) symbol as shown in the terminal command yo need to activate it. So for activating the virtual environment on your terminal type following command:

source bin/activate

You will see the change in your terminal something like this:

(trydjango) username@username:/../../sda1/project/python/trydjango$ 

Now you are all set to write your program or execute your python code from this directory. If you simply import the libraries/modules and run the code, it will execute. After doing your code on this your machine, now if you want to move ahead and continue to program in some other workstation, then you need to do the following:

  1. Commit your code in your version control management service(GitHub, GitLab,)
  2. Download VIrtualEnv on the other workstation
  3. Activate the Workstation
  4. Clone the repository and start working again.

Thank you for going through this easy and elaborated blog on Virtual Environment. This blog is a part of our Django Tutorial Series. You can find more tutorials which are listed synchronously in the following blog:

Python Django Tutorial For Beginners

%d bloggers like this: