always lowercase py files, always
- August 13th, 2012
- By Rob Galanakis
- Write comment
In my line of work I deal with junior programmers constantly. One of the most common non-code mistakes I see them make is using capital letters in their python file names. I’m not sure why it happens so often, to such a diverse crowd, but I guess it comes down to bad examples, and stylistic preference.
Except it isn’t stylistic preference. And unlike my rant about tabs-vs-spaces, it isn’t easy to fix either.
Imagine a simple scenario (note, this only applies to Windows- I should mention in my line of work we are usually on Windows) :
- You create a file, myModule.py, you put some awesome code in it, and check it into source control (probably Perforce but AFAIK any source control has the same problems).
- A while later, you rename it to mymodule.py, fix up your code references, and check it in.
- The code breaks on all your users machines, and you can’t clean it up without manual intervention, or changing your module to be called something else.
Did you see what happened? When you checked in the code, everyone synced down ‘myModule.py.’ When you changed the capitalization, everyone else’s VCS synced down the new file contents but, because the file was already there, didn’t delete myModule.py. So everyone else got the new code calling ‘import mymodule’ but keeps the old filename ‘myModule.py’. Fixing this problem either requires removing the file from people’s client and force syncing (manually or by mass-deploying a fixup script), or just renaming ‘mymodule’ to something else entirely.
The problem stems from python, which is case-sensitive, relying on the filenames of paths, which rely on Windows, which is case insensitive. Don’t bother blaming anyone, it’s just the way it is.
So the way to deal with it? Always use lowercase, which is exactly what the PEP8 style guide suggests.