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 env
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 https://files.pythonhosted.org/packages/a9/4f/8a247eee2958529a6a805d38fbacd9764fd566462fa0016aa2a2947ab2a6/Django-3.0.5-py3-none-any.whl Collecting asgiref~=3.2 (from django) Using cached https://files.pythonhosted.org/packages/68/00/25013f7310a56d17e1ab6fd885d5c1f216b7123b550d295c93f8e29d372a/asgiref-3.2.7-py2.py3-none-any.whl Collecting sqlparse>=0.2.2 (from django) Using cached https://files.pythonhosted.org/packages/85/ee/6e821932f413a5c4b76be9c5936e313e4fc626b33f16e027866e1d60f588/sqlparse-0.3.1-py2.py3-none-any.whl Collecting pytz (from django) Using cached https://files.pythonhosted.org/packages/e7/f9/f0b53f88060247251bf481fa6ea62cd0d25bf1b11a87888e53ce5b7c8ad2/pytz-2019.3-py2.py3-none-any.whl 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() '3.0.5'