How to find python excecutable path in bge? (import sys no work)

oh look, another multiprocess thread again :spin::spin::spin:
It must be this time of the year again!

Basically I want to reset the python sys.excecutable to be the installed python installation source platform independently.

Mainly to launch multiprocess without wasting too time on forking​ around.

So i present a example which I blasted together:


from subprocess import Popen,PIPE


commands = [
    ["which", "python3", "python"],
    ["where", "python3", "python"],


]


def is_correct_excecutable(thing):
    return 'python' in thing.lower() and ('/' in thing or '\\' in thing)


def find_python_excecutable():  
    for command in commands:
        try:
            with Popen(command, stdout=PIPE) as proc:
                path = proc.stdout.read().decode().strip()
                if is_correct_excecutable(path):
                    return path
        except Exception as e:
            print(e)


excecutable = find_python_excecutable()
print(excecutable)


import sys
sys.executable = excecutable


from multiprocessing import Process
import os




def f(name):
    import time
    for i in range(1000):
        print('hello', name, time.time())
        time.sleep(0.5)


def main():
    p = Process(target=f, args=('bob',))    
    p.start()



And the questions are:

  • Is this the easiest way?
    [LIST]

  • I don’t know if the python installed with in Blender is easy to find on all platforms.

  • Is it right to expect that most folks in BA forum have installed python on their machine?
    [/LIST]

Actually forking around is probably the way to go.

And the questions are:

  • Is this the easiest way?
    [LIST]

  • I don’t know if the python installed with in Blender is easy to find on all platforms.

  • Is it right to expect that most folks in BA forum have installed python on their machine?
    [/LIST]

The Python installed within blender is bundled on the Blenderplayer, at least the executable (actually a library), the Python library may be a little harder to find but it’s usually on the same directory than the game (since you need to copy it), tough the Blenderplayer may find it somewhere else if it’s missing on the game directory. And no, I wouldn’t assume everybody here has an independent copy of Python, I would assume that most people here uses Windows and only installed Blender, not Python. Notice that you can start the BGE on headless mode (at leas UPBGE) so you could do something with that if forking / threading doesn’t fit your needs.

But in any case using a system command for something like this doesn’t seem a good idea. Also I doubt changing sys.executable does anything, I would assume it’s a read-only variable, much more promising seems “multiprocessing.set_executable()”, though I read is useless on Linux since the method used for multiprocessing is forking anyway.

Will this allow users to use things like pyttsx without re-writing anything?

just install (module with os specific dependencies)

run your script, then you can just import normally?

pyttsx is just a Python module, you should be able to use it by just coping it in your game working directory.

Nope - binds to local OS specific stuff - youle made it so it worked by changing a few lines of code somewhere in the driver I think*

the files are availible in my B_Pecs Wip thread.

Do you want to use a different version of Python or do you want to have additional libraries in your python search path?

@elmeunick9
As python is embedded into the blender process I thought that forking would be more effort, but might look further into it then…
I have caused even a fork bomb on someones computer that way in the past :smiley:
And yeah, forgot to add ‘multiprocessing.set_executable()’ to the example. that would have made more sense.

@Monster
I want A python path, don’t really care which.
Multi process starts by default an instance of sys.excecutable which in blender is blender instead of python.
So it would open the modeling software gui instead of a background python process.

That specific OS stuff probably are shared libraries, installing them solves the problem, if you don’t want to force the end user to insteall them manually you can distribute them as well, in this case tough you may need to change the path from where they’re loaded from the driver scripts. Whatever, what you’re trying to do is not related with this post.

@VegetableJuiceF

If the multiprocessing library opens a completly new instance of blenderplayer you should try the headless mode, assuming that Python is installed is in my opinion not an option.

I thought with this trick, blender may be able to import stuff directly from the system python path which is pretty convenient…

anyhow, this is why we ask questions, to find out answers.

Blender does import stuff from the python path. However the python path is the path where the python interpreter you’re using takes it’s libraries from, in blender that’s on the Python directory of your game working directory (when distributed) or your blender installation (when launching from inside Blender). If you put any library there you will efectively be able to use it (just like if you where putting it on the working directory, so not providing any advantage over it).

For testing/debug, you may add a sperate python path (from another Python interpreter for example) with sys.path.append(), so you could install modules with pip and test them, tough maybe you can also install modules with pip in BGE through the Blender console (not tested).

It’s fine to ask BPR, I’m not criticizing you, at least not today.