guide virtual-env   0   562
Virtual Environment in Python - A Pocket Guide

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.

virtualenv --version
rana@Brahma ~$ virtualenv --version
15.1.0


Using virtual environment:

Create a virtual environment:

virtualenv project_1_venv


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
or

virtualenv -p /usr/bin/python3 venv3


To work inside a virtual environment, we need to activate it.

source virtual_env_folder/bin/activate


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 deactivate  command.   



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:

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…
Installing django…
Collecting django
  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]…
Installing requests…
Collecting requests
  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! ? 

guide virtual-env   0   562

Related Articles:
How to develop a distributable Django app to block the crawling IP addresses
How to create reusable django app which can block crawling IPs from accessing your app. Creating distributable python package and upload on pypi....
Automatically updating Django website hosted on PythonAnyWhere server with every git push
How to automate the process of updating website hosted on python any where server everytime you commit and push code to git repository....
Using Docker instead of Virtual Environment for Django app development
Using docker instead of virtual environment for Django app development, starting with Dockerization of your Django application. Django with Docker....
How to create completely automated telegram channel with python
Creating a completely automated telegram channel to generate and post content using python code on regular basis. Automating the Telegram channel using python script...

0 thoughts on 'Virtual Environment In Python - A Pocket Guide'
Leave a comment:


*All Fields are mandatory. **Email Id will not be published publicly.


SUBSCRIBE
Please subscribe to get the latest articles in your mailbox.



Recent Posts:






© pythoncircle.com 2018-2019