Python - virtualenv (Python Environment)

Card Puncher Data Processing

About

Python Virtual Environments allow Python packages to be installed in an isolated location for a particular project, rather than being installed globally.

By default (without virtual environment), all package are installed by default in the same default global site-packages directory (for instance /usr/lib/python2.7/site-packages). Therefore this is not possible to have system wide two package with two different versions. virtual environment resolves this problem.

Structure

A virtual env will install:

Structure:

  • ENV/lib/ - virtual env library
  • ENV/include/ - virtual env library
  • ENV/lib/pythonX.X/site-packages/. - Linux - installed package
  • ENV/lib/site-packages/. - Windows - installed package
  • ENV/bin - executables on Linux
  • ENV/Scripts - executables on Windows

Executables are noticeably a new python, setuptools, pip,…

The python in your new virtualenv is effectively isolated from the python that was used to create it.

Example PySpark, the package are installed in

  • venv\lib\site-packages\pyspark\

Management

Create

There are two tools for creating Python virtual environments:

  • venv is available by default in Python 3.3
  • virtualenv needs to be installed separately, but supports Python 2.7+ and Python 3.3+

venv

venv is available by default in Python 3.3

Example and usage:

  • venv
python3 -m venv ENV
source ENV/bin/activate

virtualenv

virtualenv needs to be installed separately, but supports Python 2.7+ and Python 3.3+

Steps:

pip install virtualenv
  • Creation with virtualenv
# Usage:
# virtualenv [--system-site-packages] ENV
#     where: 
#        * ''ENV'' is a directory to place the new virtual environment.
#         * ''%%--system-site-packages%%'' to inherit packages from the [[..:grammar:regular#global|global site package dir]]
cd c:\tmp\
mkdir myVenv
virtualenv myVenv
Using base prefix 'c:\\python37-32'
New python executable in c:\tmp\myVenv\Scripts\python.exe
Installing setuptools, pip, wheel...
done.

Remove

(ENV)$ deactivate
  • Remove
rm -r /path/to/ENV

Run a script

Directly

Running a script with #! /path/to/ENV/bin/python would run that script under this virtualenv’s python.

If you directly run a script or the python interpreter from the virtualenv’s bin/ directory (e.g. path/to/ENV/bin/pip or /path/to/ENV/bin/python-script.py) then sys.path will automatically be set to use the Python libraries associated with the virtualenv. But, unlike the activation scripts, the environment variables PATH and VIRTUAL_ENV will not be modified.

Activate

Before running a script, you may activate it. Activating the virtual environment means running an activate script.

The script will:

  • change your PATH so its first entry is the virtualenv’s bin/ directory
  • set the VIRTUAL_ENV variable
  • modify your shell prompt to indicate which environment is currently active. To disable this behaviour, see VIRTUAL_ENV_DISABLE_PROMPT

Example:

  • Bash
source /path/to/ENV/bin/activate
deactivate
  • Dos
\path\to\env\Scripts\activate.bat
\path\to\env\Scripts\deactivate
  • Powershell
Set-ExecutionPolicy AllSigned
\path\to\env\Scripts\activate.ps1

Documentation / Reference





Discover More
Card Puncher Data Processing
Conda - Environment

A conda environment is a directory that contains a specific collection of conda packages that you have installed. The following subdirectories comprise the default Anaconda environment: active...
Idea Python Interpreter Venv
PySpark - Installation and configuration on Idea (PyCharm)

Installation and configuration of a PySpark (Spark Python) environment on Idea (PyCharm) You have already installed locally a Spark distribution. See Install Anaconda 2.7 (3.7 is also supported)...
Card Puncher Data Processing
Python - Dependency Management

See: syntax When you install a package, or declaring a dependency, you need to specify an package id that's known...
Card Puncher Data Processing
Python - Packages (Archive, Distribution)

in Python Packages (or better Distribution package) are the result of the packaging of a project that may contains one or more (regular) package regular packageregular package A package is a compressed...
Python Build Artifact Setup Py
Python - Regular Package (Shipping | Packaging)

and in Python How to release regular package adapted from (they are using the setuptools framework) With python 3. where: setup.py...
Card Puncher Data Processing
Python - Script

in Python are source file that can be run. On Linux, begin your scripts with your interpreter. See or You can find them by executing the whereis commando: sys.argv[0] is the script name...
Python Test Idea
Python - Test

in Python From the standard library: Example: doctest describe test that look like interactive Python sessions with: the command the expectations...
Card Puncher Data Processing
Python - The python interpreter

The main function of the python interpreter is to run a script. See script Global or in a virtual environment see
Idea Pipenv Sdk
Python - pipenv (dependency framework)

pip+venv = pipenv pipenv is a dependency framework for Python. See it will install a Example The created Pipfile (configuration)....



Share this page:
Follow us:
Task Runner