Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Evaluate an expression automatically while installing required Python packages
ResourceFunction["PythonEvaluateWithDependencies"][spec,cmds] evaluates the Python commands cmds in a system satifying the specification spec and tries to install any required Python packages that are not yet installed. | |
ResourceFunction["PythonEvaluateWithDependencies"][expr] evaluates the expression expr that calls ExternalEvaluate to execute Python commands. | |
ResourceFunction["PythonEvaluateWithDependencies"][…,info] returns the specified additional information info. |
All | an Association of the result and a list of installed packages |
None | only the result of evaluation (default) |
The Python package "camelcase" used in this example is not installed in this system:
In[1]:= | ![]() |
Out[1]= | ![]() |
Evaluate the Python code, installing the package automatically:
In[2]:= | ![]() |
Out[2]= | ![]() |
In[3]:= | ![]() |
Out[3]= | ![]() |
The package is now installed:
In[4]:= | ![]() |
Out[4]= | ![]() |
Uninstall the package to clean up:
In[5]:= | ![]() |
Out[5]= | ![]() |
Evaluate an expression that contains a call to ExternalEvaluate:
In[6]:= | ![]() |
In[7]:= | ![]() |
In[8]:= | ![]() |
Out[8]= | ![]() |
Clean up:
In[9]:= | ![]() |
Out[9]= | ![]() |
Install as many Python packages as necessary to successfully evaluate an expression:
In[10]:= | ![]() |
Out[10]= | ![]() |
In[11]:= | ![]() |
Out[11]= | ![]() |
Uninstall the packages to clean up:
In[12]:= | ![]() |
Out[12]= | ![]() |
Obtain both the result of evaluation and a list of installed packages:
In[13]:= | ![]() |
Out[13]= | ![]() |
Uninstall the added packages to clean up:
In[14]:= | ![]() |
Out[14]= | ![]() |
Use the option "NameSubstitutions" to install a specific version of a package:
In[15]:= | ![]() |
Clean up:
In[16]:= | ![]() |
Out[16]= | ![]() |
PythonEvaluateWithDependencies is most useful when the Python code requirements are not known:
In[17]:= | ![]() |
Start a session and make an inventory of installed packages:
In[18]:= | ![]() |
Out[18]= | ![]() |
In[19]:= | ![]() |
Evaluate the black box code:
In[20]:= | ![]() |
Out[20]= | ![]() |
See the newly installed package:
In[21]:= | ![]() |
Out[21]= | ![]() |
Uninstall it:
In[22]:= | ![]() |
Out[22]= | ![]() |
End the session:
In[23]:= | ![]() |
Use the resource function CreatePythonVirtualEnvironment to ensure that packages installed by PythonEvaluateWithDependencies will not interfere with your working Python system:
In[24]:= | ![]() |
Out[24]= | ![]() |
In[25]:= | ![]() |
Out[25]= | ![]() |
In[26]:= | ![]() |
The "requests" package is installed in your new virtual environment:
In[27]:= | ![]() |
Out[27]= | ![]() |
Typically, environments are used in multiple Wolfram Language sessions and deleted afterwards:
In[28]:= | ![]() |
Out[28]= | ![]() |
In[29]:= | ![]() |
The resource function PythonPackageInstall can be used instead of PythonEvaluateWithDependencies when the package requirements are known beforehand:
In[30]:= | ![]() |
In[31]:= | ![]() |
Out[31]= | ![]() |
In[32]:= | ![]() |
Out[32]= | ![]() |
Uninstall the package:
In[33]:= | ![]() |
Out[33]= | ![]() |
PythonEvaluateWithDependencies fails if a missing Python package cannot be installed:
In[34]:= | ![]() |
Out[34]= | ![]() |
This work is licensed under a Creative Commons Attribution 4.0 International License