Blog of Rob Galanakis (@robgalanakis)

always lowercase py files, always

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) :

  1. 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).
  2. A while later, you rename it to mymodule.py, fix up your code references, and check it in.
  3. 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.

7 thoughts on “always lowercase py files, always

  1. robert says:

    Seems to me the problem rather stems from the revision control system which seems to rely on the OS for file name comparison (but maybe there’s a hidden switch somewhere deep inside P4 to change this?). It’s a bit strange that P4 works like that – given that there’s surely enough devs who use it with Python, mixed OS environments or other case sensitive languages that care about file names.

    I never ran into this problem myself, but thanks for the warning.

  2. GDorn says:

    Agreed. While you should follow PEP8 unless you have a good reason not to, the real WTF here is your VCS.

  3. David says:

    In both git and svn all you have to do is rename your file using your VCS, rather than windows.

    According to the Perforce documentation, ( http://kb.perforce.com/?article=007 ) the situation is the exact same.

    This is really a question of people not using their VCS properly , and not a python problem at all.

    1. David: That’s true, but only for the local user. When other users sync that rename (integrate/delete), it will not change the casing.

  4. James Thiele says:

    Also some versions of Mac OS X have case insensitive file names.

  5. Spencer Luebbert says:

    It totally makes sense to do all lower case from the start. But I don’t understand why this needs to be fixed after the file has already been integrated into a bunch of code. I mean aren’t you manufacturing the problem by trying to change case in the first place? Why not just change it back to myModule to avoid fixing it locally on everyone’s machine. Or if your really really must make it lower case, just change the name completely to like mybettermodule, so that source control doesn’t get confused.

  6. Spencer, it doesn’t have to be months down the line- the system may still be in development, someone catches the module name during code review but it’s been checked in, and now this inactive module on other people’s machines is cased incorrectly. Better- like you say- to go all case insensitive from the start, and make sure people know it (I’m not advocating trying to go in and fix otherwise working code).

    Otherwise, if you do have to change it, you should do like you say (and I mention in the article), rename the module something else entirely.

Leave a Reply