Question

If I am right in understanding, Python compile files are cross platform. So why are most libraries released requiring a build and install?

Is it laziness on the part of the distributer, or am I wrong in saying they could simply distribute the pyc files? If this isn't the case, how do I distribute a python script file that has libraries as prerequist without requiring the user to build and install the libraries?

Was it helpful?

Solution

Because the format of the contained code can change with each (major) Python release, while source code at least can be compatible. The format can also differ between implementations (of which there are several, the two most well-known are CPython and PyPy.

See this article for more information about the internal structure of .pyc files.

OTHER TIPS

They are cross platform, but not cross-version and not cross-implementation. In other words, different CPython versions could have trouble with one and the same .pyc file.

And if you look at other implementations such as PyPy, IronPython, Jython etc., you won't have any luck with .pyc files at all.

Besides, .pyc files correspond to one .py file. What you (probably) have in mind are libraries which include a part written in C. They must be compiled to platform-dependent files (.pyd, .dll, .so, etc.) in order to work. As not many developers have all supported platforms available, they provide it as source and leave it to the user's system to compile it.

The process of creating a .pyc file from a .py file is done transparently by the interpreter. The reason some installers generate them at install time is purely to avoid them having to be generated the first time the script is run - usually for speed reasons, but also so that the .pyc can end up in a system-wide (non-user-writable) directory. Unlike compiled languages, or even Python modules written in C, it is not a technical requirement that these files exist in the first place - if it was, you wouldn't be able to put your own scripts directly into python and have them run without a compile step.

Any library that strictly requires a build step before it is installed certainly has a component that compiles to native code - which certainly isn't cross-platform. For any pure-python library, it is always sufficient to put the module's source directory tree somewhere in your PYTHONPATH, having copied it directly from the archive you downloaded it in.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top