Featured

Thursday, 27 September 2018

Create, Activate, Manage & Deactivate Virtual Environments in Python

This post walks you through creating and managing virtual environments in Python 2.7.

Python language is powerful and used for many different purposes in real time. To develop applications, we need to have isolated environments to manage different working copies of components. The virtual environment here comes handy to solve this concern.

A virtual environment is a named, isolated, working copy of Python that maintains its own files, directories, and paths so that you can work with specific versions of libraries or Python itself without affecting other Python projects. Virtual environments make it easy to cleanly separate different projects and avoid problems with different dependencies and version requirements across components.

In native python, 'virtualenv' is a third party tool used to create isolated Python environments, it is defaults to installing pip into all created virtual environments

What is 'virtualenv' and why you should we use it?

virtualenv is a tool which allows us to make isolated python environments. How does it making isolated python environments help us? Imagine you have an application that needs version 2 of a library, but another application requires version 3. How can you use and develop both these applications? If you install everything into /usr/lib/python2.7/site-packages (or whatever your platform’s standard location is), it’s easy to end up in a situation where you unintentionally upgrade an application that shouldn’t be upgraded.

It is where 'virtualenv' comes into play. It creates isolated environments for your python application and allows you to install python libraries in that isolated environment instead of installing them globally.

Create virtual environments in native python

In order to install it just type this command in the shell or windows command prompt

$ pip install virtualenv

To create virtual environment

$ virtualenv your_env_name

To activate or get into the environment

$ source bin/activate

Install packages as usual

$ pip install arrow

To deactivate or exit from an environment

$ deactivate

Create virtual environments for python with conda

Check the Python version before you create virtual environment

$ conda create -n your_env_name python=x.x anaconda

Note: where x.x is the version of installed Python

Press y to proceed. This will install the Python version and all the associated anaconda packaged libraries at "path_to_your_anaconda_location/anaconda/envs/your_env_name"

To activate your virtual environment.

To activate or switch into your virtual environment, simply type the following where 'your_env_name' is the name you gave to your environment at creation.

$ source activate your_env_name

For Windows:

> activate your_env_name

Activating a conda environment modifies the PATH and shell variables to point to the specific isolated Python set-up you created. The command prompt will change to indicate which conda environemnt you are currently in by prepending (your_env_name). To see a list of all your environments, use the command conda info -e.

Install additional Python packages to a virtual environment.

To install additional packages only to your virtual environment, enter the following command where yourenvname is the name of your environment, and [package] is the name of the package you wish to install. Failure to specify “-n yourenvname” will install the package to the root Python installation.

$ conda install -n [your_env_name] [package]

Deactivate your virtual environment.

To end a session in the current environment, enter the following. There is no need to specify the envname - which ever is currently active will be deactivated, and the PATH and shell variables will be returned to normal.

$ source deactivate

For Windows:

> deactivate

Delete a no longer needed virtual environment.

To delete a conda environment, enter the following, where yourenvname is the name of the environment you wish to delete.

$ conda remove -n yourenvname -all

Related info

The conda offical documentation can be found here.

No comments:

Post a Comment