Including local Python libraries in Unreal python

Hi there.

We are currently working on a commandlet that depends on several other libraries (For example: numpy and google protobuf). They are already installed in Windows Python environment (Python version 2.7) and currently it is possible for us to have them by manually add the hardcoded path in Unreal’s python shell by running sys.path.import(“MyPath”). (Reference this link) Or configuring the DefaultEngine.ini by adding

[/Script/PythonScriptPlugin.PythonScriptPluginSettings]
+AdditionalPaths=(Path=“MyPath”)

The problem raises when we want to distribute these commandlets and have other people use them. We can’t hardcode it anymore so we started to find ways to let unreal recognize libraries already installed in the machine. After some experiencing, we realized that Unreal seems to be holding its own “Python Environments” and have a different sys.path compared to operation system’s python environment. Shall we instead put all libraries in the place where Unreal can recognize it? Or is there a way to let unreal read what libraries OS already have?

Thank you tech arts!

Hi!
I used Python for a coupla of UE4 projects using python and external libs.
I don’t know if this could solve your problem, because I don’t know how to detect libraries (but I suppose you can copy/paste them under unreal python folder).
BTW, in order to avoid hardcoding of paths, I used an environment variable.

Under the Content folder I created a Python folder, then placed there a startup_script.py file.
The script was basically this:

import sys
import os
my_libs_path = os.getenv("ENV_VARIABLE_NAME")
if my_libs_path is not None and my_libs_path not in sys.path:
    sys.path.append(my_libs_path )

That’s it.
If the Python plugin is active in your project, this script will be automatically executed and the path added.
I know, it’s still require the setting of a system variable but could be fine?

1 Like

Hi

Thanks for your reply. We used to be doing something similar, looking through system path and if our path is not there manually add it. We also posted questions in UDN and they suggested us to have libraries to be placed somewhere within your project/plugin that the UE4 Python environment has access to.

So our ultimate solution becomes place all libraries under MyCustomEngine/Engine/Content/Python and distribute our Unreal engine. As Unreal is smart enough to know where it is and where to fetch libraries. (You can reference this link) as

The Unreal Editor automatically adds several paths to this sys.path list:

  • The Content/Python sub-folder under your Project’s folder.
  • The Content/Python sub-folder in the main Unreal Engine installation.
  • The Content/Python sub-folder under each enabled Plugin’s folder.
  • The Documents/UnrealEngine/Python folder inside your user directory. For example, on Windows 10, this is equivalent to C:/Users/Username/Documents/UnrealEngine/Python

Yiming

image.png