How to Activate Python Virtual Environment

Posted on

Introduction

This article is discussing a simple topic. It is showing how to activate python virtual environment. There are specific steps to achieve it. The following are those steps for activating the python virtual environment :

  1. First of all, before activating the python virtual environment, it must exist in the first place. In order to create the python virtual environment so that it will exist, a certain package must available. The ‘virtualenv’ package must exist in the operating system. Read the article with the title of ‘How to Install virtualenv tool for Creating Python Virtual Environment in Ubuntu Linux 18.04’ in this link for more information. That package is necessary to create python virtual environment.

2. Next step, after the ‘virtualenv’ package is available in the operating system, just create the python virtual environment. There are several useful references for creating the python virtual environment. It is also useful for checking the availability of the python virtual environment. Just read the article with the title of ‘How to Create a Python Virtual Environment’ in this link. Another link for creating python virtual environment using python 3.6 is also available in the article with the title of ‘How to Create a Python 3.6 Virtual Environment’ in this link.

3. If the python virtual environment is available, just type the following command activate the python virtual environment :

source /python-virtual-environment-path/bin/activate

Referring to the above command pattern, the following is a real example for activating the virtual python environment :

root@hostname ~# source /root/python-env/bin/activate
(python-env) root@hostname ~# 

Checking the Python Virtual Environment

As in the above output command, after activating the python virtual environment, the bash prompt is changed. There is an additional characters in the front of it. In this context, it is ‘(python-env)’. It is actually referring to the name of the python virtual environment. The exact location is actually in the ‘/root’ folder and the name of the python virtual environment is ‘python-env’. It has the same name with the folder representing the python virtual environment. Finally, in order to make sure whether the python virtual environment is using the correct python version, just execute the following command :

(python-env) root@hostname ~# python --version
Python 3.6.8
(python-env) root@hostname ~# 

There is also another way to check the python virtual environment by searching which python available in it. Just execute the following command :

(python-env) root@hostname ~# which python
/root/python-env/bin/python
(python-env) root@hostname ~#

Leave a Reply