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.
A virtual env will install:
Structure:
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
There are two tools for creating Python virtual environments:
venv is available by default in Python 3.3
Example and usage:
python3 -m venv ENV
source ENV/bin/activate
virtualenv needs to be installed separately, but supports Python 2.7+ and Python 3.3+
Steps:
pip install 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.
(ENV)$ deactivate
rm -r /path/to/ENV
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.
Before running a script, you may activate it. Activating the virtual environment means running an activate script.
The script will:
Example:
source /path/to/ENV/bin/activate
deactivate
\path\to\env\Scripts\activate.bat
\path\to\env\Scripts\deactivate
Set-ExecutionPolicy AllSigned
\path\to\env\Scripts\activate.ps1