Reputation: 43
I'm basically building a GUI app in Godot 4, which uses a python script in the background to do some things. It passes variables from Godot to the script with arg-parse. To do these things, I created a Virtual Environment in the File System of Godot. Something similar shown in this video. (res:// -> Python_files -> venv). The python script that I am using needs pandas to work. Pandas needs numpy. Therefore I pip installed numpy into the venv. Upon switching back to Godot, this is what it looked like: Picture of Errors and (re)import Godot keeps re-importing parts of the numpy library, all the time, showing the "(re)import-popup window, and cluttering the venv with umath-validation files (in markdown)
Error importing 'res://PythonFiles/venv/lib64/python3.10/site-packages/numpy/core/tests/data/umath-validation-set-cos.csv'.
Error importing CSV translation: expected 3 locale(s), but the '## +ve denormals ##' key has 0 locale(s).
Error importing 'res://PythonFiles/venv/lib64/python3.10/site-packages/numpy/core/tests/data/umath-validation-set-exp.csv'.
Error importing CSV translation: expected 3 locale(s), but the '## +ve denormals ##' key has 0 locale(s).
Error importing 'res://PythonFiles/venv/lib64/python3.10/site-packages/numpy/core/tests/data/umath-validation-set-log.csv'.
Error importing CSV translation: expected 3 locale(s), but the '## +ve denormals ##' key has 0 locale(s).
Error importing 'res://PythonFiles/venv/lib64/python3.10/site-packages/numpy/core/tests/data/umath-validation-set-sin.csv'.
Even a new empty Godot project without any code, gives the same errors. Following these steps will most likely lead to the same outcome, as my code shows:
Ubuntu Version 22.04 (also tried on Mac-OS 13.xx) Python Version 3.10 Godot Version 4.1.1
To solve this problem, I tried different versions of Numpy which are compatible with Pandas, it did not resolve anything. I also tried the same process on a mac. Still no difference.
I assume that there must be a compatibility problem between Godot and numpy.
Upvotes: 2
Views: 683
Reputation: 40315
Do not add your Python code et.al. to the Godot project. You don't need your external programs to be in res://
to work. In fact, it is a bad idea.
Launching an executable form res://
will work when you are launching your application from the editor, but when the application is exported, res://
does not map to the project folder anymore, instead it maps to a virtual path inside a .pck
file, and your OS won't be able to launch anything from there (and that is assuming they are there, which requires to import them, and export them - which is possible, but a waste of effort).
I would suggest to use user://
, as they are guaranteed to work on the exported application, regardless of operating system. However, that does not make testing and deployment easy.
See File paths in Godot projects.
For other people reading this…
Notice that OS.execute
does not require an absolute path. As long as the OS can resolve the name of what you are trying to execute, it should work.
I also want to mention that it is also possible to launch another program using OS.create_process
, or even OS.shell_open
, but only OS.execute
will let you get the stdout
and stderr
.
The approach I've taken for non-game applications made in Godot is to figure out the path of your Godot application in the system and use that:
if OS.has_feature("template"):
var current_path := OS.get_executable_path().get_base_dir()
The call OS.has_feature
("
template
")
will return true
if the application is exported. In which case OS.get_executable_path
().
get_base_dir
()
will be the folder where the application is stored (if the application is not exported - i.e. running from the editor - it will be the folder of the Godot executable, which presumably you don't want to use).
Ok, but how do you test from the editor? One option is to hard-code the path, but barring that, you might resolve the project folder:
var current_path := ""
if OS.has_feature("template"):
current_path = OS.get_executable_path().get_base_dir()
else:
current_path = ProjectSettings.globalize_path("res://sub_folder/")
Here ProjectSettings.globalize_path
translates "res://" and "user://" paths to OS paths.
Ah, but I said to not add your Python code to the Godot project! But I didn't say to not put it in the project folder. To do that, you can add a .gdignore
so the Godot does not include anything inside of that folder to the project. See Project organization.
Automating the deployment of said folder is another matter, I suggest you use shell script that calls Godot via command line to export the application, and also copies the necessary files.
Barring that, you could always have a way to configure the path. For example, you might have a config file in user://
(created by the application if it does not exist) where you store the external path you will use, and offer UI to the user to configure it.
Upvotes: 1