1

Core Problem:

I've run into situations multiple times where pyc files break my local application (especially unit tests).

For example I have a folder utils/ which contains a poorly named sqlalchemy.py (which just contains utilities for sql alchemy). I add another file utilities/whatever.py which contains

import sqlalchemy.orm.session

and that breaks because well...the python2 importer at least thinks "I'm going to look at the relative path first". So I rename or delete utilities/sqlalchemy.py and everything works hunky dory.

Until my teammates pull down changes and everything breaks. Because while their copy no longer has utilities/sqlalchemy.py, it still has the git ignored utilities/sqlalchemy.pyc file

What I Want:

Is there a way to get the python importer to ignore pyc files? I would of course only want this to be active locally when running unit test, so I'm hoping for like an environment variable or a configuration for the python importer, but hoping other people have run into this problem enough that there's some sort of "quasi-official" solution.

Note that I specifically don't want to do something like find . -name '*.pyc' -delete in a post-checkout git hook since our repo is large enough that this takes several minutes and would really slow us down every time someone switches branches.

George Mauer
  • 117,483
  • 131
  • 382
  • 612
  • 1
    Might you consider not generating the `.pyc` files in the first place? https://stackoverflow.com/questions/154443/how-to-avoid-pyc-files - this would only be set in environments where you're concerned with this problem...not in production. – CryptoFool Mar 11 '22 at 18:06
  • Hmm, not as slick cause doesn't solve things for everyone (and some tools might want this) but thats worth considering thanks @CryptoFool! – George Mauer Mar 11 '22 at 18:10
  • Shouldn't it be enough that they delete the offending .pyc file(s) *once*? (note they can easily do this using the `git clean` command and do not need to use a `find` invocation.) How often do you name your local modules the same as globally installed packages? – mkrieger1 Mar 11 '22 at 18:12
  • @mkrieger1 in a sufficiently large organization where some people might not be pulling certain branches for weeks or months...yeah, its a problem and one that people who aren't very experienced at this don't even know where to start with solving it since the error message is so confusing and stuff just suddenly doesn't work (but everyone else says it does). It also doesn't have to be the same as globally installed ones, can just be the same as modules/packages at your project root – George Mauer Mar 11 '22 at 18:14
  • its not clean, but would grabbing all .pyc files in a list with path.glob(), renaming these to .temp files at the start of the unittest, running the unittest, then renaming them back to .pyc files at the end of the unittest work? – Hersh Joshi Mar 11 '22 at 18:21
  • @HershJoshi it would add a ton of time to unit tests which we try hard to keep – George Mauer Mar 11 '22 at 18:23

0 Answers0