Module Import Errors in Python when Executing a Program

I ran into a weird issue today with a python program where even though I have the pyyaml module installed python would complain that ImportError: No module named yaml when I tried to execute my program.

I have ran into this in the past and the solutions posted online vary greatly and usually didn’t work for me. I think I have found the root cause of this and wanted to quickly share it here.

I am running Python 2.7.9 on OS X Mavericks. Pyyaml was installed via pip and works just fine.

The interesting part about this is that when I execute the program by running python program.py everything works fine. If I chmod +x program.py to make it executable and then try to execute it by running ./program.py then this issue begins to arise.

The key issue happens to be the header that tells the OS where to find python.

When you run python from a shell in OS X it is looking to /usr/local/bin/python to find and execute python. You can verify that this is true by running which python in a terminal. When you execute a python program without explicitly calling python then it will look for python in your file header.

The first line of a file typically looks like this #!/usr/bin/python. This was the cause for the descrepancy in my case. Since the python that is called when I say python is different than the python that I defined in my header the module was not able to load.

Pip installs all modules wherever your main python is. The fix for this issue is to change the header from #!/usr/bin/python to #!/usr/bin/env python This way it will look to whatever the default python installation is, and in the event that you are running a virtual environment there will not be any clashing.

This seems like one of those tricky bash gotcha’s that happen sometimes. Using #!/usr/bin/env python by default is a safe bet to make sure you don’t run into these types of issues.

If you made it this far, you should probably follow me on twitter. 🙂

This entry was posted in Hacking and tagged . Bookmark the permalink.

Leave a Reply

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