Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the limitations of distributing .pyc files?

Tags:

python

pyc

I've started working on a commercial application in Python, and I'm weighing my options for how to distribute the application.

Aside from the obvious (distribute sources with an appropriate commercial license), I'm considering distributing just the .pyc files without their corresponding .py sources. But I'm not familiar enough with Python's compatibility guarantees to know if this is even workable, much less whether it's a good idea or not.

Are .pyc files independent of the underlying OS? For example, would a .pyc file generated on a 64-bit Linux machine work on a 32-bit Windows machine?

I've found that .pyc file should be compatible across bugfix releases, but what about major and minor releases? For example, would a file generated with Python 3.1.5 be compatible with Python 3.2.x? Or would a .pyc file generated with Python 2.7.3 be compatible with a Python 3.x release?

Edit:

Primarily, I may have to appease stakeholders who are uncomfortable distributing sources. Distributing .pyc's without sources may give them some level of comfort, since it would require the extra step of decompiling to get at the sources, even if that step is somewhat trivial. Just enough of a barrier to keep honest people honest.

like image 878
leedm777 Avatar asked Jul 06 '12 19:07

leedm777


People also ask

Are .PYC files necessary?

pyc file is generated, there is no need of *. py file, unless you edit it.

Are PYC files cross platform?

Because . pyc files are platform independent, they can be shared across machines of different architectures.

What is the difference between .py and .PYC files?

py files contain the source code of a program. Whereas, . pyc file contains the bytecode of your program.

What is the use of .PYC file in Python?

The py_compile module provides a function to generate a byte-code file from a source file, and another function used when the module source file is invoked as a script.


3 Answers

For example, would a file generated with Python 3.1.5 be compatible with Python 3.2.x?

No.

Or would a .pyc file generated with Python 2.7.3 be compatible with a Python 3.x release?

Doubly no.

I'm considering distributing just the .pyc files without their corresponding .py sources.

Python bytecode is high-level and trivially decompilable.

like image 193
Cat Plus Plus Avatar answered Sep 29 '22 19:09

Cat Plus Plus


You certainly could distribute the .pyc files only. As Cat mentioned, no it would not be compatible with different major version of Python. It might prevent some people from viewing the source code, but the .pyc files are very easy to decompile. Basically if you can compile it, you can decompile it.

You could use a binary packager like py2exe / py2app / freeze. I've never tried them but someone could still decompile them if they wanted to.

like image 23
reptilicus Avatar answered Sep 29 '22 17:09

reptilicus


As Cat said, pyc files are not cross version safe. Though what you're trying to hide from the users determines what you need to do.

As for source code, there is no good way to hide Python source code in a distributed application. If you just trying to hide specific details you could pack those into a C extension -- which would be much harder to decompile.

So if you're worried about code use, put a license attached to the code for no-use or translate the sections you don't want stolen to a compiled language. If you just want code to not be obviously Python, you can create a binary executable that wraps the Python code (though doesn't hide the actual details if someone extracts them from the file).

like image 44
Pyrce Avatar answered Sep 29 '22 19:09

Pyrce