Install Python modules and packages
Important
Support for the Python Scripting package is scheduled to end as of Unity 6.1.
Extend Python abilities in your Unity project with additional Python modules and packages:
- Access your own Python modules and packages.
- Manually install extra Python packages with pip.
- Automatically install extra Python packages via a
requirements.txt
file.
Access your own Python modules and packages
To make your own Python modules and packages accessible from the Unity Editor:
From the Unity Editor main menu, select Edit > Project Settings, then select Python Scripting.
In the Package Directories list, select the + (plus) button to add a new entry.
In the new entry, set the path to the folder containing your Python modules or packages.
Select Apply.
Unity adds this list of paths to the sys.path
variable and makes your own Python modules discoverable from the Unity Editor. You can then import them in a script, including via the Python Script Editor.
Note
- If you modify or remove an existing path in this list, you need to restart the Unity Editor for it to take effect.
- Paths are treated as verbatim strings. You need to expand environment variables or use the
~
(on macOS and Linux) denoting the home directory.
Manually install a Python package with pip
To manually install a Python package with pip through the terminal:
From the Unity Editor main menu, select Edit > Project Settings, then select Python Scripting.
Select Launch Terminal.
Use pip to install the desired Python package.
For example:python -m pip install PySide2
.
Unity installs the Python package in the following folder of your Unity project: Library/PythonInstall/Lib/site-packages
.
Automate Python packages installation
Use this method for example when you need to share your Unity project with other co-workers and make sure everyone is using the same set of Python modules and packages with compatible versions.
To make the Unity Editor automatically install a predefined set of Python packages at start-up:
In your Unity project, in the
ProjectSettings
folder, create a newrequirements.txt
file.List the desired Python packages in this new file as per the pip requirement file format.
Open (or restart) your Unity project to apply any changes to the
requirements.txt
file.
Warning
If you're using a requirements.txt
file, be aware that Python packages installed individually from the terminal will be uninstalled the next time you open your Project unless they are also listed in the requirements.txt
file.