What not to do when importing Python modules
May 24, 2006 by Isaac
Let's see, this would be stupid mistake number 23,320,594,283 for me so far this year....
If you haven't yet discovered it, IPython is the greatest thing since sliced bread. Come to think of it, I think it's better than sliced bread. Anyway, in my typical Python development cycle I have one console open with IPython running and another with a normal command prompt. I also have my favorite editor (emacs) running with the source code under development. In this way I can use the awesome features of IPython, including tab completions. So, in my typical development model I experiment in IPython to figure out the exact command syntax I need or to try things which I am not all that familiar with. Tab completion comes in very handy here.
Anyway, I have a module that I am experimenting with and after starting IPython I typed "import" followed by the first few characters of the module name and hit tab. IPython dutifully completed the line and I hit enter. I immediately got an error message "no module named py." What in the world? After wasting about an hour poking around my system and hunting for different settings, I realized the stupidity of my mistake. An import takes the modules name minus the .py extension! Duh! I knew that, but in my haste at hitting tab I didn't bother to carefully observe the completion of the line. Once I removed the .py extension from the import it was happy.
So, we have:
import module.py <------ don't do this
vs.
import module <------ this is what you want
Moral: Be careful with that tab key!
Posted in 
content rss
