I am trying to run a python script from a editor script.
You can run a python script from the editor with
command = {"python", "./editor-script/scripts/main.py"}
if the script can be found by the system. Meaning, you can not use it as a dependency, if your script will at some point be imported as a dependency then you need to run it as text. This is done like this.
command = {"python", "-c", editor.get("/editor-script/scripts/main.py", "text")}
My problem is that Defold will pick up the 2.7 from the macOS., this can be verified by command = {"python", "-c", "import sys; print(sys.version)"}
. This means that if I am relying on external libraries installed with pip
I first need to extend the PATH.
To figure out where python is I have a shell script that will source the .bash_profile
before running the python script, a user can then extend the PATH themselves.
It simply looks like this
#!/bin/bash
source "$HOME/.bash_profile"
python "$@"
I then call this by running
local function command_workaround()
if editor.platform == "x86_64-darwin" then
return "./editor-script/scripts/extend_path.sh"
end
return "python"
end
---- Using the command
command = {command_workaround(), "./editor-script/scripts/main.py"}
As I mentioned before this works fine when having it locally but it will fail if I use it as a dependency.
Error:
ERROR:EXT: Check Libraries for Updates's "run" in /editor-script/check_library.editor_script failed:
ERROR:EXT: Cannot run program "./editor-script/scripts/extend_path.sh" (in directory "/Users/mattias.hedberg/Documents/repositories/project"): error=2, No such file or directory
As we said this is because when used as a dependency the relative path is no longer correct.
One would think I could do
editor.get("/editor-script/scripts/extend_path.sh", "path")
to first get the (absolute) path but it reports the same relative path (expected).
"/editor-script/scripts/extend_path.sh"
If we try to load the shell script as text
local function command_workaround()
if editor.platform == "x86_64-darwin" then
return editor.get("/editor-script/scripts/extend_path.sh", "text")
end
return "python"
end
We get
ERROR:EXT: Cannot run program "#!/bin/bash
ERROR:EXT: source '$HOME/.bash_profile'" (in directory "/Users/mattias.hedberg/Documents/repositories/project"): error=2, No such file or directory
Which makes sense, the text string is not a command, normally this can be worked around with a call with eval
. But putting it as a command gives an error
command = {"eval", command_workaround(), "./editor-script/scripts/main.py"}
------
ERROR:EXT: Check Libraries for Updates's "run" in /editor-script/check_library.editor_script failed:
ERROR:EXT: Cannot run program "eval" (in directory "/Users/mattias.hedberg/Documents/repositories/project"): error=2, No such file or directory
And putting it into a the .sh
script gives ann error too
eval "#!/bin/bash
\nsource '$HOME/.bash_profile'
\npython "$@""
-----
ERROR:EXT: Check Libraries for Updates's "run" in /editor-script/check_library.editor_script failed:
ERROR:EXT: Cannot run program "eval "#!/bin/bash
ERROR:EXT: \nsource '$HOME/.bash_profile'
ERROR:EXT: \npython "$@""" (in directory "/Users/mattias.hedberg/Documents/repositories/project"): error=2, No such file or directory
Which leads me to my question: Do anyone know a way to get PATH extended by sourcing .bash_profile
and then calling a python script inside that extended shell?