Encrypt python files

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed May 6 07:50:17 EDT 2015


On Wed, 6 May 2015 07:45 pm, Albert-Jan Roskam wrote:

> I used the marshal module before as a faster alternative to shelve (I
> marshalled a huge dictionary). I always understood marshal files require
> the *exact* same interpreter version (incl. built number). Do the same
> limitations apply for .pyc files? Isn't it the same format?

marshal is currently up to version 4 of the (otherwise undocumented) file
format. Obviously that's much less than the number of minor versions of
Python, so, no, marshal does not change every release.

https://docs.python.org/3/library/marshal.html

.pyc files, on the other hand, have a magic number which changes for each
minor release of Python. I think that, in principle, it could change in a
point release (say, between 3.4.1 and 3.4.2) but in practice I don't
believe that has ever happened. I think it is safe to assume that pyc files
will be portable across any minor release (e.g. 3.4.x for any x) but not
across changes to the minor or major release.

I don't think that is an outright promise, but it is a strong convention.

And of course, other implementations may not even use .pyc files, if they
don't use the same sort of byte code. E.g. Jython compiles to JVM byte
code:

foo.py
foo$py.class


Nick Coghlan has a very good blog post about the uses of pyc only
distributed software:

http://www.curiousefficiency.org/posts/2011/04/benefits-and-limitations-of-pyc-only.html

And Ned Batchelder has a good discussion of their internals:

http://nedbatchelder.com/blog/200804/the_structure_of_pyc_files.html



-- 
Steven




More information about the Python-list mailing list