The Biggest Python Trap!

If I had a Dollar for every hour I’ve wasted debugging this issue in Python, I’d be … uhh… well a little more well off than I am now.

The way that Python handles modules is fantastic, but sometimes if you don’t pay attention, you can end up wasting time debugging an issue when the error message leads you down the wrong path.

Often – and this blog is probably more a reminder for myself than anyone else – I create small test scripts to play around with new modules. In the most recent example, I started experimenting with Kivy – the Python UI library.

I created a script called “kivy.py” and copied the hello world example code and ran it.

Traceback (most recent call last):
  File "kivy.py", line 1, in <module>
    from kivy.app import App
  File "C:\Python\hal\kivy.py", line 1, in <module>
    from kivy.app import App
ModuleNotFoundError: No module named 'kivy.app'; 'kivy' is not a package

Of course, the first thought is that the module was not properly installed. It could take some time (as is usually the case with me) to realize that the actual problem is that I named the test script (kivy.py) the same as the module I am trying to import:


from kivy.app import App
from kivy.uix.button import Button

class TestApp(App):
    def build(self):
        return Button(text='Hello World')

TestApp().run()

Silly, silly mistake, but somehow I do this ALL THE TIME. I think it’s time to start naming my scripts “trying_out_kivy.py” or something 😀

Leave a Reply

Your email address will not be published. Required fields are marked *