Import pywhatkit and KeyError: 'display'

My app runs fine in local host, but when deploying I got this error message:


Thanks for your help!

Hi @3jcn, welcome to the Streamlit community!

If I had to guess, I would suspect that pywhatkit expects to be able to open a browser, but Streamlit sharing is a “headless” server (i.e. it doesn’t have local display capabilities). So it looks for what sort of display might be available, and since there isn’t one, a KeyError is thrown.

It does look like that’s the case:

Thanks, Randy. How’s to solve the problem?

Hi Randy,
I disabled pywhatkit to avoid that problem, but I got another. Here is the my app url, can you take a look and find out what’s wrong. I have no problems running it on localhost.
https://share.streamlit.io/3jcn/intro-physical-science-bot/main/main.py
Thanks,

ModuleNotFoundError: No module named ‘comtypes’
but two folders: ‘comtypes’ and ‘pyttsx3’ are located in my ‘/site-packages/’

It appears that comtypes is a Windows-specific package (Streamlit sharing are Debian images). For pyttsx3, it appears that it has some linux-specific install instructions:

If those are really the issue, then you would need to add a packages.txt file, with each library that needs to be apt-installed on a separate line. You can see an example of using a packages.txt file here (that example is for ODBC drivers, but the overall structure is the same):

Best,
Randy

My OS is Windows 10. I think the problem is the way “Streamlit Sharing” access local library. All required libraries are there in local “site-package” folder, that’s why app works in localhost, but somehow “Streamlit” does not find them. Could you please test my files with your computer?

https://share.streamlit.io/3jcn/spmarket/main/sp_main.py

In general, Python works how it works, and Streamlit sharing isn’t doing anything non-standard. When you have a requirements.txt file, we install it the same way you might do on any other Linux machine.

What behavior are you seeing that doesn’t work? Your Streamlit sharing link appears to be running.

Best,
Randy

Thanks, Randy. The link is an example for my point: if I used:
“from keras.layers import Dense, LSTM
from keras.models import Sequential”
my app worked on localhost, but not online (module Sequential not found).
Then I modified:
“from tensorflow.python.keras.layers import LSTM, Dense
from tensorflow.python.keras.models import Sequential”
my app works for both localhost and online.
For this app: https://share.streamlit.io/3jcn/intro-physical-science-bot/main/main.py
I can’t find another way to use “pyttsx3”.