Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python *.py, *.pyo, *.pyc: Which can be eliminated for an Embedded System?

To squeeze into the limited amount of filesystem storage available in an embedded system I'm currently playing with, I would like to eliminate any files that could reasonably be removed without significantly impacting functionality or performance. The *.py, *.pyo, and *.pyc files in the Python library account for a sizable amount of space, I'm wondering which of these options would be most reasonable for a Python 2.6 installation in a small embedded system:

  1. Keep *.py, eliminate *.pyc and *.pyo (Maintain ability to debug, performance suffers?)
  2. Keep *.py and *.pyc, eliminate *.pyo (Does optimization really buy anything?)
  3. Keep *.pyc, eliminate *.pyo and *.py (Will this work?)
  4. Keep *.py, *.pyc, and *.pyo (All are needed?)
like image 207
Lance Richardson Avatar asked May 12 '09 00:05

Lance Richardson


People also ask

What is .py and .PYC in Python?

. 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?

pyc file contains the “compiled bytecode” of the imported module/program so that the “translation” from source code to bytecode can be skipped on subsequent imports of the *. py file. Having a *. pyc file saves the compilation time of converting the python source code to byte code, every time the file is imported.

How do you stop a .PYC file?

Suppressing the creation of __pycache__ Alternatively, you can set PYTHONDONTWRITEBYTECODE environment variable to any non-empty string. Again, this will prevent Python from trying to write . pyc files. Note that both approaches are equivalent.

What is a .PYO file?

A PYO file represents the bytecode file that is read/written when any optimization level is specified (i.e., when -O or -OO is specified).


2 Answers

http://www.network-theory.co.uk/docs/pytut/CompiledPythonfiles.html

When the Python interpreter is invoked with the -O flag, optimized code is generated and stored in ‘.pyo’ files. The optimizer currently doesn't help much; it only removes assert statements.

Passing two -O flags to the Python interpreter (-OO) will cause the bytecode compiler to perform optimizations that could in some rare cases result in malfunctioning programs. Currently only doc strings are removed from the bytecode, resulting in more compact ‘.pyo’ files.

My suggestion to you?

Use -OO to compile only .pyo files if you don't need assert statements and __doc__ strings.

Otherwise, go with .pyc only.

Edit

I noticed that you only mentioned the Python library. Much of the python library can be removed if you only need part of the functionality.

I also suggest that you take a look at tinypy which is large subset of Python in about 64kb.

like image 109
Unknown Avatar answered Oct 02 '22 15:10

Unknown


Number 3 should and will work. You do not need the .pyo or .py files in order to use the compiled python code.

like image 42
AlbertoPL Avatar answered Oct 02 '22 13:10

AlbertoPL