Tuesday, March 6, 2018

How to use virtual invironment in python 3.6

A Virtual Environment, allows to create an isolated working copy of Python. It is quite common situation  when Python applications  use packages  that  are not part of the standard library. It happens that  multiple applications may require different version of the same library. In other words- single installation of  Python  will not be able to serve  all the possible requirements  for   applications. This issue  can be solved by utilizing  Virtual environments. Virtual environment is directory that contains  all the Python installation  and all the required  packages.  Different Python application may use  dedicated  virtual environments.

Starting Python 3.6  -  virtual environment  creation module shipped  as part of Python installation.

Here is example - how to create virtual environment using Python 3.6

python -m venv env

This command creates new forlder "env" in current directory (if directory not yet exists). It also create subdirectories in "env" directory that   containing a copy of the Python interpreter, the standard library,  supporting files, etc.

After  virtual environment is created- it needs to be activated:
Depend on  OS type- it can be done in several ways.

For Windows:
.\env\Scripts\activate.bat

For Linux:

./env/Scripts/activate.sh

After  virtual environment is activated-  shell will show  activated virtual  environment as a prefix in command prompt: (venv).

Now we can install  all the required libraries for our current  Python application.
For example:

python -m pip install beautifulsoup4


python -m pip install lxml

All the installed packages  will be stored inside of "venv" folder  and not overlap/affect any other Python  applications.
v

No comments:

Post a Comment