+1 vote

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!

asked Dec 15, 2018 in Engine by johnygames (1,574 points)

1 Answer

+2 votes
Best answer

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.

answered Dec 15, 2018 by Ertain (1,253 points)
selected Dec 16, 2018 by johnygames

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?

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. ;)

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.

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.

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.