In almost every article, we recommended the use of virtual environment for developing any Python or Django project.
In this article, we will briefly cover the virtual environment in python, installation and usage.
What is a Virtual Environment:
Virtual environment is an isolated python environment which can be created using virtualenv python tool. This virtual environment contains all the packages that a python package would require.
Python project running in virtual environment does not use the system wide installed python package.
Installing Virtual Environment:
We can install virtual environment using pip.
pip install virtualenv
You can confirm the installation by running below command.
rana@Brahma ~$ virtualenv --version
Using virtual environment:
Create a virtual environment:
This will create a virtual environment with name
project_1_env and install python binaries in this folder. This will use the system's default python' version.
To create virtual environment with specific python version use below command.
virtualenv -p /usr/bin/python2.7 venv2
virtualenv -p /usr/bin/python3 venv3
To work inside a virtual environment, we need to activate it.
Navigate inside the virtual environment 'bin' directory and check if new copies of python binaries are present. If python binaries are soft-links to system python binaries, use the
--copies flag in above command.
virtualenv -p /usr/bin/python3 --copies venv3
Now you may install python packages inside this isolated environment using pip install command.
Once you are done working with your project, you may deactivate the virtual environment by
Deleting virtual environment:
To delete a virtual environment simply remove the directory.
rm -rf virtual_env_directory
Freezing the requirements:
When you deploy your project on any other server like PythonAnyWhere
, you need to replicate the virtual environment.
You can not simply copy the folder as it will break the things. Also this is not a good idea as the size of the folder will be large.
We will first list all the packages installed in virtual environment and dump in a file.
pip freeze > requirement.txt
Now commit this file along with your code.
On production server, clone the code repository and create a virtual environment. Once virtual environment is activated, install all the dependencies using the pip install command below.
pip install -r requirement.txt
This will create the exact copy of virtual environment you were working on development server.
Pipenv is a dependency manager for python projects. It is similar to Symfony's composer and node.js's npm.
Its a higher lever tool than pip.
We recommend to use pipenv for managing your virtual environments.
With pipenv, we need not to manage
requirement.txt file separately.
As per pienv readthedocs:
It automatically creates and manages a virtualenv for your projects, as well as adds/removes packages from your Pipfile as you install/uninstall packages. It also generates the ever–important Pipfile.lock, which is used to produce deterministic builds.
Install pipenv using pip.
pip install pipenv
Creating virtual environment using pipenv:
Go to your project directory.
Install python packages using pipenv command.
rana@Brahma: ~$ cd /tmp
rana@Brahma: tmp$ mkdir django_project_1
rana@Brahma: tmp$ cd django_project_1/
rana@Brahma: django_project_1$ pipenv install django requests
Creating a virtualenv for this project…
?Using base prefix '/usr'
New python executable in /home/rana/.local/share/virtualenvs/django_project_1-NloTdiFm/bin/python3
Also creating executable in /home/rana/.local/share/virtualenvs/django_project_1-NloTdiFm/bin/python
Installing setuptools, pip, wheel...done.
Virtualenv location: /home/rana/.local/share/virtualenvs/django_project_1-NloTdiFm
Creating a Pipfile for this project…
Using cached Django-2.0-py3-none-any.whl
Collecting pytz (from django)
Using cached pytz-2017.3-py2.py3-none-any.whl
Installing collected packages: pytz, django
Successfully installed django-2.0 pytz-2017.3
Adding django to Pipfile's [packages]…
Using cached requests-2.18.4-py2.py3-none-any.whl
Collecting chardet<3.1.0,>=3.0.2 (from requests)
Using cached chardet-3.0.4-py2.py3-none-any.whl
Collecting certifi>=2017.4.17 (from requests)
Using cached certifi-2017.11.5-py2.py3-none-any.whl
Collecting idna<2.7,>=2.5 (from requests)
Using cached idna-2.6-py2.py3-none-any.whl
Collecting urllib3<1.23,>=1.21.1 (from requests)
Using cached urllib3-1.22-py2.py3-none-any.whl
Installing collected packages: chardet, certifi, idna, urllib3, requests
Successfully installed certifi-2017.11.5 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22
Adding requests to Pipfile's [packages]…
PS: You have excellent taste! ?