Friday, October 5, 2018

Python Virtual Environment

A virtual environment is a named, isolated, working copy of Python that 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.

Activating a conda environment modifies the PATH and shell variables to point to the specific isolated Python set-up you created.


1. virtualenv

virtualenv is a very popular tool that creates isolated Python environments for Python libraries. It works by installing a bunch of files in a directory (eg: env/), and then modifying the PATH environment variable to prefix it with a custom bin directory (eg: env/bin/). An exact copy of the python or python3 binary is placed in this directory, but Python is programmed to look for libraries relative to its path first, in the environment directory.

Once activated, you can install packages in the virtual environment using pip.


Install virtualenv:
  $ pip install virtualenv

Create a virtual environment:
  $ virtualenv yourenvname

Activate the virtual environment:
  $ source yourenvname/bin/activate
To leave my virtual environment:
  $ deactivate

To install all the required packages specified by the file requirements.txt:
  $pip install -r requirements.txt

If you want to have all the installed packages in a requirements.txt
  $ pip freeze > requirements.txt

To delete a virtual environmrent
 $ rm -r /path/to/yourenvname


2. conda
The conda command is the preferred interface for managing installations and virtual environments with the Anaconda Python distribution.

To see a list of available python versions:
 $ conda search "^python$"

Create a virtual environment with python version x.x for your project
 $ conda create -n yourenvname python=x.x anaconda

To activate or switch into your virtual environment,
 $ source activate yourenvname

To see a list of all your environments,
 $ conda info -e


To install additional packages only to your virtual environment
 $ conda install -n yourenvname [package]

Failure to specify “-n yourenvname” will install the package to the root Python installation.

To end a session in the current environment
 $ source deactivate

No comments:

Post a Comment