nice info - import ensurepip


after we have pip in blender, it looks like we can install python libs right into blender?

I almost have it working

but not quite.

beware - locks up kinda when running this from printing / cycling errors

hold escape to exit (for a while sometimes)

I already use pip with blender. On linux, you get blender to use the system python and you’re set. On windows (or if you can’t get it to use system python) you can use the python binary in the 2.78 folder to install pip by running There are instructions I put on on blend stackexchange a week or so ago

Why are you trying to install dependencies while the game is running? Surely it’s better to install those into your local blender, then when you export it they are distributed with the game automatically?

that does not work with OS specific libs, like pyttsx*

this way, the end user just needs to run B_pecs as a admin 1 time to get the libs.

also btw, can you tell me what I am doing wrong?
ensure pip runs, but it looks like my install thing is failing*

that does not work with OS specific libs, like pyttsx

Yes, you have to make distribution-specific releases, just like with any exported game.

pip itself isn’t OS specific, so you can have that permanently installed - no need to install it every time.
Also, you will have very strange problems running pip from a blender instance. It is better to run it just using the python binary provided with blender. Why? pip is multithreaded, and for some reason, on windows, this means it opens lots of blender windows and fails to install things!
What would I suggest? A script called "install-deps.bat’ (and similarly ‘’ on 'nix) that invokes the pip commands. Then, in your game, if it fails to import a module have it display a screen prompting the user to run that script.

1 Like

do you think if you get time you could post a example and tag me?

From experience, it’s better to assume you cannot make any changes to a users system after installation, so bundle everything you need with the game, or get the installer to download/install any extra dependances.

User Account Control on Windows by default will prevent you installing anything into the installation directory unless you’re running the program as Admin (don’t do that).

hi, im trying to use pyttsx3 in blender game but i have a lot of problems.
first in the python IDLE
error like module win32api not found
or something like win32com
then i installed pywin32 with pip install from cmd
and all works in the python IDLE.
jumping into blender i first had error when i tried to import pyttsx3
so i copied it in the blender folder version im using and its ok
but another error appeared
the same in the idle, something like win32api
i tried to copy something in the blender directory
now module pythoncom non found
can someone help me pls?
im using upbge 0.2.0 but i tried with other version too
i tried to use pyttsx and pyttsx3 both

the script im using is this just for testing:

import pyttsx3
engine = pyttsx3.init()
engine.say("I am talking now ")

thx all and sorry for necroposting but it is all day im looking for a solution

1 Like

Using blender’s python:

python -m ensurepip
python -m pip install pyttsx3

Part of your issue is that the python executable that blender uses and the one you develop on outside are two different executables, and they do not share any package/libraries: install something with one, the other won’t see it.

You should also understand by now that you don’t want to play “copy the folders by hand”, because when installing a package, maybe other dependencies are also required. pip knows, let pip do.

Oh and lastly, distributing such a game is not really straight forward, unless you distribute an executable version, in which case you will have to run pip inside your release before uploading it for others to use.

On the following page they have a section to fix potential issues with your library:

1 Like

I have already read about it and i was able to fix the problem in the idle but seems wont work in blender

seems i got it, what i did , (after pip install pyttsx3 and pywin32), it was to copy site-package folder from python and pasted into my blender version site-package folder
the only thing now is that if you run the game, stop it and run it again without close blender it gives an error and doesn’t work honestly dunno why, but it works fine in runtime.

mmm another problem, while pyttsx3 speaking all game angine freezes mmmm
looking at the BPR file in the first post i suppose he wanted to use subprocess for this issue, dunno.

Most of the time when you program, you expect that every line of code is completed in order. Start one line, finish it, move on to the next.

It sounds like your package is doing something that is not quick, and Python is making everything wait for it to finish.

The solution is to do it asynchronously, usually using threads.

maybe a callback and ensure the thing you are calling is at the last line?

if 'Called' not in own:
    #call pyttsx and set Called =True


and some sort of callback from the pyttsx that removes called on finish?

Not sure why that would make any difference. If the function takes 3 seconds to complete, then it is going to hold up BGE from moving to the next frame for 3 seconds regardless of where in the script it is written.

Something as silly as:

global_speak_lock = Lock()

def run_me_in_a_thread():
    with global_speak_lock:
        if 'speaking' in object:
            raise RuntimeError('I am already speaking, please wait.')
        object['speaking'] = True
        del object['speaking']

That’s just one way I guess.

edit: in my example, one could use the lock in a better way, oh well…

i used some different tts like win32com.client or tts.sapi with no errors in some case and the problem still remain, seems bge cant support this thing, it always freezes and restart after speech, im tring to use thread or multiprocessing but seems isnt the way but im quite noob so…maybe not.
maybe opening a shell in subprocess dunno

i found this topic, it is a “little bit” outdated but maybe can help

maybe using socket, why not

I solved that by stopping the thread when I stopped the BGE. A script attached to the ESC key will do the trick.

Note that this is only a ‘problem’ when running it inside Blender. If it’s the standalone player, then Windows/OSX will clean up orphaned threads when the game finishes.

On my end, I monitor some dummy object garbage collection in order to detect engine shutdown
(even in the editor):

Which then gracefully shutdowns the thread:

Working with threads in the BGE requires to be careful for sure :slight_smile:

1 Like