Wolfram Research

Function Repository Resource:

CreatePythonVirtualEnvironment (1.1.0) current version: 1.1.1 »

Source Notebook

Deploy a Python virtual environment

Contributed by: Wolfram Staff

ResourceFunction["CreatePythonVirtualEnvironment"]["dir"]

creates a Python virtual environment with a directory name "dir", using the default Python system.

ResourceFunction["CreatePythonVirtualEnvironment"][spec,"dir"]

creates a Python virtual environment using the Python system specified by spec.

Details

Virtual environments are Python's way to deal with conflicting package requirements. Each virtual environment has its own Python binary and its own independent set of installed packages.
ResourceFunction["CreatePythonVirtualEnvironment"] creates a new Python system, makes it ready for immediate use and returns a UUID, which is a key to the system in the FindExternalEvaluators["Python"] dataset.
If it fails, ResourceFunction["CreatePythonVirtualEnvironment"] returns a Failure object.
In ResourceFunction["CreatePythonVirtualEnvironment"][spec,"dir"], the Python system specification spec is the same as in StartExternalSession.
spec must refer to a registered evaluator or give an explicit path to a Python executable.
The convention for specifying a directory name "dir" is the same as in CreateDirectory; File["dir"] is also supported.
The repository function DeletePythonVirtualEnvironment deletes the Python system created with ResourceFunction["CreatePythonVirtualEnvironment"].

Examples

Basic Examples (5) 

Create a new virtual environment:

In[1]:=
id = ResourceFunction["CreatePythonVirtualEnvironment"]["experiments"]
Out[1]=

The environment is registered and ready to use:

In[2]:=
FindExternalEvaluators["Python"][id]
Out[2]=

Start a new Python session in the new environment:

In[3]:=
KeyTake[%, {"System", "Target"}] // Normal
Out[3]=
In[4]:=
session = StartExternalSession[%]
Out[4]=

Evaluate code in the session:

In[5]:=
ExternalEvaluate[session, "2+2"]
Out[5]=

End the session and delete the environment:

In[6]:=
DeleteObject[session]
In[7]:=
ResourceFunction["DeletePythonVirtualEnvironment"][id]
Out[7]=

Scope (3) 

Create a new virtual environment using the default Python system:

In[8]:=
id = ResourceFunction["CreatePythonVirtualEnvironment"][
  "default_python"]
Out[8]=
In[9]:=
FindExternalEvaluators["Python"][id]
Out[9]=

Delete the environment:

In[10]:=
ResourceFunction["DeletePythonVirtualEnvironment"][id]
Out[10]=

Create a new virtual environment based on another Python version:

In[11]:=
id = ResourceFunction[
  "CreatePythonVirtualEnvironment"][<|"System" -> "Python", "Version" -> "3.9.1"|>, "python_env_3.9.1"]
Out[11]=
In[12]:=
FindExternalEvaluators["Python"][id]
Out[12]=

Delete the environment:

In[13]:=
ResourceFunction["DeletePythonVirtualEnvironment"][id]
Out[13]=

Create a virtual environment outside the current working directory:

In[14]:=
id = ResourceFunction["CreatePythonVirtualEnvironment"][
  FileNameJoin[{$UserBaseDirectory, "Applications", "MyExperiments", "venv"}]]
Out[14]=
In[15]:=
FindExternalEvaluators["Python"][id]["Target"]
Out[15]=

Delete the environment:

In[16]:=
ResourceFunction["DeletePythonVirtualEnvironment"][id]
Out[16]=

Applications (7) 

Virtual environments are perfect for experimenting with a new system version or a new version of a complex package with multiple dependencies without affecting other Python systems. Here is a set of packages installed in the default Python instance:

In[17]:=
ResourceFunction["PythonPackageList"][]
Out[17]=

Create a new virtual environment:

In[18]:=
id = ResourceFunction["CreatePythonVirtualEnvironment"][
  "python_env_experiments"]
Out[19]=

At the outset, the new environment has only the default package set:

In[20]:=