How to import python modules using Godot Python?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By toblin

I’m trying to use Cuda GPU accelerations for some heavy parts of code through the Godot Python bindings. I’ve installed the python bindings using the assetlib, and it seems to work. I am now struggling with importing python modules. Note that I’m using anaconda3 for my python packages.

The question on how to import python modules have been discussed here:
https://forum.godotengine.org/37679/possible-u%20se-python-modules-and-libraries-with-godot-yes-how?show=37724#c37724

But so far they have not assisted me. The concrete example of how to import is to copy python folders into addons/pythonscript/x11-64/lib in the Godot project folder. This does not appear to work however.

So (as the title reads) does anyone have any ideas on how to import python modules in Godot Python?

:bust_in_silhouette: Reply From: toblin

To be able to import python libraries, it seems to work to add a path to where the python libraries are on disk in the Project Settings> General> Filesystem> Python Script> Path using the Godot editor.

I’m using conda, and I’ve added

/home/[username]/anaconda3/lib/python38.zip
/home/[username]/anaconda3/lib/python3.8;
/home/[username]/anaconda3/lib/python3.8/lib-dynload;
/home/[username]/anaconda3/lib/python3.8/site-packages;

which seems to make packages such as math, numpy, etc importable in Godot. The file references are on Ubuntu linux.

To find where your python libraries are, open a python shell and do:

import sys
print(sys.path)

which should give you the paths that your python installation uses to locate python libraries for import.

You can also import your own python modules by adding them to the project folder. Just make sure that res:// or any other local Godot file reference exists in the Project Settings> General> Filesystem> Python Script> Path in the Godot Editor.

Note: this method makes the project unable to export later… so a better method is probably preferred.

toblin | 2020-09-30 07:10