I was trying to use the playsound function from the playsound module in Python 3. I obtained an mp3 file of a fire alarm sound from http://soundbible.com/1656-House-Fire-Alarm.html to test and saved as siren1.mp3.
Minimal code:
from playsound import playsound
playsound('siren1.mp3')
Initially, I tried to run the minimal, 2 line Python code in Thonny and could not get it to work, as it gave "No module named gi" error.
It led me to try various things, finding that some of them worked and others did not. Because I eventually managed to get playsound to work in Thonny, my question is not how to get playsound working.
On Stack Overflow I find many related or similar questions of the form "x works on y but not on z" and "No module named xx" error. As a relatively new Python user, what I am looking for is guidance on how to understand the different paths taken by Python to look for modules etc. depending on how one executes Python code.
Here are the experiments I did and my observations:
When I ran the minimal code in Thonny (Thonny version 2.1.16, Python version 3.6.7, Tk version 8.6.8), it failed with "No module named gi" error.
The same Python program worked correctly when I ran it in IDLE (IDLE version 3.6.7, Python version 3.6.7, Tk version 8.6.8) and siren noise was played.
In Ubuntu (18.04) terminal,
$ which python
showed: /usr/bin/python$ python3 ./playSiren.py
ran correctly and produced sound
Now executed python3 at the command prompt
$ python3
Python 3.6.7 (default, Oct 22 2018, 11:32:17) [GCC 8.2.0] on Linux
Now I copied and pasted the two Python code lines and ran them on the Python prompt. Again the code ran correctly and produced sound.
- I added a shebang line to the minimal code and changed it to an executable and then ran it from the Ubuntu prompt.
Again the code ran correctly and produced sound
- When I ran:
sudo apt-get install python3-gi
,
I get the message:
python3-gi is already the newest version (3.26.1-2ubuntu1).
In IDLE:
>>> import gi
>>> gi
<module 'gi' from '/usr/lib/python3/dist-packages/gi/__init__.py'>
sys.path
in IDLE:
/home/pi/.local/lib/python3.6/site-packages
/home/pi/Python Learning
/usr/bin
/usr/lib/python3.6
/usr/lib/python3.6/lib-dynload
/usr/lib/python3/dist-packages
/usr/lib/python36.zip
/usr/local/lib/python3.6/dist-packages
In Thonny:
>>> import gi
Traceback (most recent call last):
File "<pyshell>", line 1, in <module>
ModuleNotFoundError: No module named 'gi'
sys.path
in Thonny:
/home/pi/.thonny/Python36/lib/python3.6/site-packages
/home/pi/.thonny/jedi_0.11.1
/home/pi/Python Learning
/usr/lib/python3.6
/usr/lib/python3.6/lib-dynload
/usr/lib/python3/dist-packages/thonny/shared
/usr/lib/python36.zip
- On Thonny I appended the following to the
sys.path
:
sys.path.append('/usr/lib/python3/dist-packages/gi/__init__.py')
sys.path.append('/usr/lib/python3/dist-packages/gi/')
Still, I was getting No module named 'gi'
error on Thonny.
If IDLE shows gi module to be in /usr/lib/python3/dist-packages/gi/__init__.py
, I expected Thonny also to be able to find gi when I appended this to the sys.path
. Why did it not work?
Then I appended the following:
sys.path.append("/usr/lib/python3/dist-packages")
Now, there was no "No module named 'gi'" error anymore and play sound correctly produced the siren sound!