0

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:

  1. 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.

  2. 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.

  3. In Ubuntu (18.04) terminal, $ which python showed: /usr/bin/python

    $ python3 ./playSiren.py

ran correctly and produced sound

  1. 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.

  1. 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

  1. 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
  1. 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!

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Ajith
  • 63
  • 1
  • 7
  • Does this answer your question? [Error Python PlaySound No module named 'gi'](https://stackoverflow.com/questions/53365209/error-python-playsound-no-module-named-gi) – Tomerikoo Oct 05 '21 at 15:15

2 Answers2

2

I assume you have installed Thonny+Python bundle from Thonny's homepage. In this case Thonny uses its own separate Python interpreter, which has its own packages.

Tweaking sys.path to include another interpreter's packages, is not reliable. It may work for Python-only packages, but it definitely won't work if the other interpreter is of different (eg. 3.6 vs 3.7) and the package is (partially) implemented in C.

You should either install all required packages for Thonny's Python ("Tools => Manage packages" would install them per-user, which means they are shared among all interpreters of same version) or you make Thonny use your system interpreter for running the programs (Tools => Options => Interpreter). I recommend the latter.

Aivar
  • 6,814
  • 5
  • 46
  • 78
  • I had used Thonny's Manage packages to install playsound. But Manage packages could not find a module called gi. However, when I selected the interpreter using Tool=>Options=> Interpreter, as you had recommended, playsound did work correctly without having to tweak sys.path. Thanks. – Ajith May 27 '19 at 23:27
0

from the official link, for ubuntu/debian users

Execute sudo apt install libgirepository1.0-dev gcc libcairo2-dev pkg-config python3-dev gir1.2-gtk-3.0 to install the build dependencies and GTK

Execute pip3 install pycairo to build and install Pycairo

Execute pip3 install PyGObject to build and install PyGObject

monti
  • 734
  • 8
  • 16