Is it possible to use Python modules and libraries with Godot? If yes, how?

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

I would like to know whether it is possible to utilize external python libraries with Godot. I have looked all around and I cannot seem to find a definitive answer. For example, would it be possible to use numpy or even PyQt? If not, does anyone know whether there will be any such capability in the future?

Thank you for your answers!

:bust_in_silhouette: Reply From: Ertain

If you didn’t know already there are Python bindings for Godot. It is possible to access external libraries, but they must be included with the Python libraries in the Godot game project directory. For example, the Godot project with the Python bindings would have the pythonscript directory, as well as the necessary *.py files. The external library is placed with the libraries the Python add-on uses. What external libraries you can use, though, seems to be limited. For example, NumPy would have to be placed with your Godot project’s Python libraries. When exporting, the C language parts of NumPy would have to be built and distributed with the project for the target platform. PyQt, though, is a bit more tricky, as this depends on the Qt bindings for Python, and will require either distribution of the Qt bindings, or detection of said bindings for the target platform. That process in itself could require platform-dependant code (e.g. a Bash shell script to detect the Qt bindings), or could even require GDnative code.

I hope that this answers your question.

Thank you for your reply!

So, technically it is doable, but the way I see it, it seems that a lot of work is required. Excuse my ignorance, but I am new to this and I do not know how much programming one would have to do in order to port the various Python libraries to the target platforms. I understand that Qt is tricky because of the reasons you mentioned. What about other libraries, such as Tensorflow and Keras? You said that “the C language parts of NumPy would have to be built and distributed with the project for the target platform”. Does this mean that I have to code some parts of my project in C or C++? Is there any chance I can make a Godot project for Windows without having to compile/code in something other than Python and GDnative?

johnygames | 2018-12-15 18:44

I do not know how much programming one would have to do in order to port the various Python libraries to the target platforms.

Lucky for you, they’ve probably been ported already. :wink:

The Godot engine add-on consists of a Python interpreter and associated libraries. To add more to this Python install, such as NumPy, the NumPy Python code and prebuilt C libraries must be added to this install (probably a matter of copying them into the same directory with the Godot-Python files). The NumPy C libraries must also be built for that platform (e.g. Windows). If the library requires a prebuilt library of Python, it may have to be linked with the Godot-Python libraries, and not any preinstalled Python binaries on Windows. The interpreter for Godot has special hooks for the engine, and so you must rely on this install. So it’s possible to use NumPy, or some similar Python software.

For something more complicated, the Godot-Python interpreter and its libraries must be the focus when building and installing software like Tensorflow. So you could use the Tensorflow libraries when writing your Godot project. But the Tensorflow libraries must know where the location of the interpreter and the Godot-Python libraries are just to work.

So you may be able to make it happen. But it will be difficult, due to how the implementation of Python for Godot is set up.

The guy who created the bindings, Emmanuel Leblond, could help you, so shoot him an e-mail (emmanuel.leblond@gmail.com) and see what he can do.

Ertain | 2018-12-15 22:14

Thanks once again!

I am a noobie and so far I have had no luck using Python with Godot (is it just me or is this topic poorly documented?). I installed Python using AssetLib from within Godot, but it doesn’t even give me the option to create a Python script. Maybe I have to use an external editor like Idle or PyCharm (?)

Anyway, I think I will follow your advice and ask Emmanuel Leblond himself. In the meantime, I will keep fiddling with the distribution. I haven’t found any hands-on tutorials and this is getting frustrating. If I manage to make progress, I may post another question with more specific information.

johnygames | 2018-12-16 00:27

It took me a while to get the Python bindings to work. Since they’re still in heavy development (and haven’t ben really tested yet), you’re going to run into some road blocks. Also, the issue had been raised, so it may get addressed in the near future.

Ertain | 2018-12-16 16:12

:bust_in_silhouette: Reply From: sustainablelab

For other people that found there way here looking to use Python in Godot, the GitHub - touilleMan/godot-python: Python support for Godot 🐍🐍🐍 project (PythonScript in the AssetLib) is active and completely usable, though it is still in beta.

I just installed it today and was able to use Python packages for USB serial communication in my Godot project. From the older posts here, it sounds like this used to be much harder than it is now. My experience with PythonScript so far is extremely positive.

Here is a quick start aimed at people familiar with Python but still new to Godot (e.g., me):

  • create a Godot project
- *ex: create a project named "Talk-to-Python"*
  • click AssetLib
  • search for PythonScript
  • download (will take a while and appears to hang, just be patient)
  • click Install
- optionally, uncheck the folders for operating systems that
  are not the operating system you're installing this on
  • save and restart:
- create a node

    - *ex: `Node2D` named `Main`*

- save the scene

    - *ex: `Main.tscn`*

- quit to Project Page

    - `Ctrl+Shift+Q`

- re-open this project

Attach a script to the Main node. In the dialog, Python is now a language option! Pick Python and let it generate the default script.

Install packages with pip from the command line as usual. Just a little setup:

  • cd into the folder for your OS, e.g. on Windows 64bit cd into the windows-64 folder
  • python.exe runs the Windows installation of Python, but I want the python.exe in this folder, so prefix with ./:
mike@HOME ~/godot-games/talk-to-python/addons/pythonscript/windows-64
$ ./python.exe -V
Python 3.8.5

The Python installed by PythonScript only has one package, godot. So first install pip as follows:

$ ./python.exe -m ensurepip
Looking in links: c:\cygwin64\tmp\tmprt3m6lj0
Requirement already satisfied: setuptools in c:\users\mike\appdata\roaming\python\py
thon38\site-packages (47.3.1)
Processing c:\cygwin64\tmp\tmprt3m6lj0\pip-20.1.1-py2.py3-none-any.whl
Installing collected packages: pip
Successfully installed pip-20.1.1

And update pip:

$ ./python.exe -m pip install -U pip

Now install whatever is needed:

$ ./python.exe -m pip install pyserial

Sorry, figured it out. Don’t know how to delete comment :frowning:

BallWave | 2021-02-20 23:38

On Ubuntu, i’m getting this error:
ModuleNotFoundError: No module named 'pip._internal'; 'pip' is not a package
Any thoughts?

MomoVR | 2022-07-21 20:23