Making good use of things
As our tools become more complex, we will definitely need various third-party Python libraries. The powerful features of these third-party libraries will also greatly enhance the functionality of our tools in the editor.
Common third-party libraries¶
- PIL(Pillow)
- imageio
- numpy
- matplotlib
- scipy-learn
- easyocr
- skimage
Recommended Practices¶
Installing Third-Party Libraries¶
- Create a virtual environment
In the following example, we will use the Python in the UE engine to create a virtual environment to facilitate our subsequent installation of third-party libraries using pip
:
virtualenv -p X:\UnrealEngine\Official\UE_5.1\Engine\Binaries\ThirdParty\Python3\Win64\python.exe ue_venv
TIP
Actually, you can use the locally installed Python as the interpreter for the virtual environment, but you need to ensure that the local Python version is consistent with the Python version in the UE engine.
Before Unreal 4.26 Preview 3, the Python version was 2.7.14
After (inclusive) Unreal 4.26 Preview 3, the Python version in UE4 is 3.7.7
Unreal 5 uses Python 3.7.9
In addition, you can use X:\UnrealEngine\Official\UE_5.1\Engine\Binaries\ThirdParty\Python3\Win64\python.exe --version
to check the Python version in the UE engine.
- Activate the virtual environment
Enter the Scripts directory of the virtual environment, execute activate.bat; MacOS users execute source activate to activate the virtual environment.
- Install third-party libraries
In the virtual environment, use pip to install third-party libraries, such as installing Pillow:
pip install pillow
Export the third-party libraries that the current environment depends on to requirements.txt:
pip freeze > requirements.txt
You can use pip install -r requirements.txt
to install from an existing requirements.txt file.
NOTE
Creating a virtual environment in PyCharm and installing various third-party libraries through the interface is also possible. Also, pay attention to the consistency of the Python version.
Using Third-Party Library Paths in UE¶
In actual use, there are usually two situations:
-
The Python tools in the project are used by multiple users in the project. In this case, we can copy the third-party libraries to the project directory
<Your_Project>\TA\TAPython\Lib\site-packages
, add them to version control, and distribute them with the project to each user. At the same time, add this directory to the Additional Path in the project's Project Settings. -
Independent developers with multiple UE projects locally. In this case, you can add the site-packages path from the virtual environment to the Additional Path in each project. This way, multiple UE projects can share the same set of third-party libraries.
TIP
Remember to ignore *.pyc files and pycache directories in your version control software.