Wolfram Research

Function Repository Resource:

PythonPackageInstall

Source Notebook

Install a Python package

Contributed by: Wolfram Staff

ResourceFunction["PythonPackageInstall"]["name"]

installs the Python package "name".

ResourceFunction["PythonPackageInstall"][{"name1","name2",}]

installs packages "namei".

ResourceFunction["PythonPackageInstall"][packages]

installs the specified versions of the packages.

ResourceFunction["PythonPackageInstall"][python,packages]

installs for the given Python installation.

Details

ResourceFunction["PythonPackageInstall"][name] attempts to install the specified package and returns a Success object if it was successfully installed and a Failure object otherwise.
If the specified package is already installed, ResourceFunction["PythonPackageInstall"] returns a Success object with the tag "PythonPackageAlreadyInstalled".
For successful installation, the Success object can contain the following key:
"InstalledDependencies" an association of installed package dependencies
"InstalledDependencies" does not necessarily list all the package dependencies but only the ones that have been installed during the current installation run.
ResourceFunction["PythonPackageInstall"][name] is effectively equivalent to executing the command line pip install name.
The string "name"can be a package name or a name-version specification in the form "package==version".
The valid specifications for packages include a rule in the form"name""version" and a list or association of name-version pairs.
In ResourceFunction["PythonPackageInstall"][python,package], the value for python can be in a form suitable for ExternalEvaluate[python,].

Examples

Basic Examples (2) 

Install a Python package:

In[1]:=
ResourceFunction["PythonPackageInstall"]["tornado"]
Out[1]=

Uninstall it:

In[2]:=
ResourceFunction["PythonPackageUninstall"][%]
Out[2]=

Scope (6) 

Install a package with dependencies:

In[3]:=
ResourceFunction["PythonPackageInstall"]["tst"]
Out[3]=

List the installed dependencies:

In[4]:=
%["InstalledDependencies"]
Out[4]=

Uninstall the package with its dependencies:

In[5]:=
ResourceFunction["PythonPackageUninstall"][%%]
Out[5]=

Install several packages:

In[6]:=
ResourceFunction["PythonPackageInstall"][{"tst", "tornado"}]
Out[6]=

Uninstall the packages:

In[7]:=
ResourceFunction["PythonPackageUninstall"] /@ %
Out[7]=

Specify the required version using the Python convention:

In[8]:=
ResourceFunction["PythonPackageInstall"]["camelcase==0.2"]
Out[8]=

Clean up:

In[9]:=
ResourceFunction["PythonPackageUninstall"][%]
Out[9]=

Specify the version as a rule:

In[10]:=
ResourceFunction["PythonPackageInstall"]["camelcase" -> "0.2"]
Out[10]=
In[11]:=
ResourceFunction["PythonPackageUninstall"][%]
Out[11]=

Or as an association:

In[12]:=
ResourceFunction["PythonPackageInstall"][<|"camelcase" -> "0.2"|>]
Out[12]=
In[13]:=
ResourceFunction["PythonPackageUninstall"][%]
Out[13]=

Make use of the same external session:

In[14]:=
session = StartExternalSession["Python"]
Out[14]=
In[15]:=
ResourceFunction["PythonPackageInstall"][session, "camelcase"]
Out[15]=
In[16]:=
ResourceFunction["PythonPackageUninstall"][session, %]
Out[16]=

End the session:

In[17]:=
DeleteObject[session]

Properties and Relations (2) 

In case of an error, the Failure object may contain a useful information returned by the underlying pip install command:

In[18]:=
ResourceFunction["PythonPackageInstall"]["foo"]
Out[19]=

PythonPackageInstall returns a Success object if the required version is already installed, or not specified:

In[20]:=
session = StartExternalSession["Python"]
Out[20]=
In[21]:=
ResourceFunction["PythonPackageInstall"][session, "camelcase"]
Out[21]=
In[22]:=
success = ResourceFunction["PythonPackageInstall"][session, "camelcase"]
Out[22]=
In[23]:=
ResourceFunction["PythonPackageInstall"][session, "camelcase==0.2"]
Out[23]=

If the versions do not match, PythonPackageInstall returns a Failure object:

In[24]:=
failure = ResourceFunction["PythonPackageInstall"][session, "camelcase==0.3"]
Out[24]=

Both Success and Failure objects with the tag "PythonPackageAlreadyInstalled" contain the location of the installed package:

In[25]:=
#["Location"] & /@ {success, failure}
Out[25]=
In[26]:=
ResourceFunction["PythonPackageUninstall"][session, "camelcase"]
Out[26]=
In[27]:=
DeleteObject[session]

Resource History

Source Metadata

Related Resources

Author Notes

Should probably have an option to update a package if it's already installed (pip install [package_name] -U). "UpdateInstalled" or some such.

License Information