How to create isolated Python environment and install Python packages

Since version 3.3, Python has come with the venv library, which provides support for creating lightweight virtual environments. Each virtual environment has its own Python binary and can have its own independent set of installed Python packages in its site directories. Using the Python venv module to create isolated Python environments allows you to use different package versions for different projects, which is far more practical than installing Python packages system-wide. Another advantage of using venv is that you won’t need any administration privileges to install Python packages.

Create an isolated environment with the following command:

[[email protected] python_env]$ python3 -m venv env
[[email protected] python_env]$ ls

This will create an env/ directory, including your Python environment. Any Python libraries you install while your virtual environment is active will go into the env/lib/python3.8/site-packages directory.

Run the following command to activate your virtual environment:

The shell prompt will include the name of the active virtual environment enclosed in parentheses, as follows:

[[email protected] python_env]$ source env/bin/activate
(env) [[email protected] python_env]$


[[email protected] python_env]$ . env/bin/activate
(env) [[email protected] python_env]$

You can deactivate your environment at any time with the deactivate command.

(env) [[email protected] python_env]$ deactivate
[[email protected] python_env]$


How to install Django with pip?

Run the following command at the shell prompt to install Django with pip :

(env) [[email protected] python_env]$ pip install django
Collecting django
Using cached
Collecting asgiref~=3.2 (from django)
Using cached
Collecting sqlparse>=0.2.2 (from django)
Using cached
Collecting pytz (from django)
Using cached
Installing collected packages: asgiref, sqlparse, pytz, django
Successfully installed asgiref-3.2.7 django-3.0.5 pytz-2019.3 sqlparse-0.3.1
(env) [[email protected] python_env]$

Django will be installed in the Python site-packages/ directory of your virtual environment.

Now check whether Django has been successfully installed. Run python on a terminal, import Django, and check its version, as follows:

(env) [[email protected] python_env]$ python3
Python 3.7.6 (default, Jan 30 2020, 09:44:41) 
[GCC 9.2.1 20190827 (Red Hat 9.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.get_version()


Leave a Reply