__pycache__, one more good reason to stck with Python 2?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Jan 19 09:42:14 EST 2011


On Tue, 18 Jan 2011 00:58:14 -0800, jmfauth wrote:

> It is now practically impossible to launch a Python application via a
> .pyc file. 


When has that ever been possible?


.pyc files are Python byte-code. You can't run them directly using Python 
(except via the import machinery), you can't run them as a script, 
they're not machine code. Unless you write a wrapper to import the file 
as a module, you can't directly execute .pyc files.


> (For the fun, try to add the "parent directory" of a cached
> file to the sys.path).

I don't understand what you're getting at there.

 
> About the caches, I'am just fearing,  they will become finally garbage
> collectors of orphan .pyc files, Python has seed/seeded(?). The .pyc
> files may not be very pleasant, but at least you can use them and you
> have that "feeling" of their existence. I my "computer experience", once
> you start to cache/hide something for simplicity, the problems start.

I will say that in general I'm not very happy with what seems like every 
second application creating huge disk caches in random locations. It's a 
PITA, and I wish they'd standardize on a single cache location, to make 
it easy to exclude them from backups, to sanitize the bred-crumbs and 
data trails applications leave, and so forth.

But having said that, the __pycache__ idea isn't too bad. If you have 
this directory structure:

./module.py
./module.pyc

and import module, the top-level .pyc file will continue to be used. If 
you delete module.py, leaving only the top-level .pyc, it will still 
continue to be used. That much hasn't changed.

The only difference is if you start with only a .py file:

./module.py

and import it, the cached version will be placed into __pycache__ 
instead. Unlike orphaned .pyc files in the top level, orphaned .pyc in 
the __pycache__ are considered stale and will be ignored. So they're safe 
to ignore, and safe to delete, at any time.

Nothing is stopping you from moving the .pyc file into the top level and 
deleting the .py file, if you so desire.



-- 
Steven



More information about the Python-list mailing list