简体   繁体   中英

Python: Programmatically compiling a Python package into pyc or pyo files

This is for my test suite.

I have an automatically-generated Python package in a temporary folder. It's all .py files. I want to programmatically compile these into (a) .pyc and (b) .pyo files. (One test will do .pyc , another will do .pyo .) This should be done with the active interpreter, of course. I do not want to import the modules, just compile.

How can I do this?

In your Python lib directory, there will be a script called compileall.py (eg, /usr/lib/python2.6/compileall.py ).

In your code, spawn (eg, by using os.spawnl ) an invocation of compileall.py pointed at the directory containing your generated code. If you invoke it using python -O it will generate .pyo files; if you invoke it using python it will generate .pyc file.

The trick, I suppose, would be to call with the right version of the Python interpreter.

compileall.py uses py_compile under the hood.

You may want to have a look at the py_compile module. Sadly it won't let you choose between pyo and pyc .

Note, that as long as there is a write permission in a directory from which a python module (*.py) is imported, the *.pyc file with the same name will be crated. Moreover, *.pyc and *.pyo do not add any performance to the program, except decreaed modules loading time.

chosing between .pyo and pyc is possible with py_compile

import py_compile

compile as simple bytecode:

py_compile.compile('sourcefilename.py', 'destinationfilename.pyc', doraise=True )

compile as optimised bytecode: both '-o' and '-oo' can be passed as parameters

py_compile.compile('sourcefilename.py', 'destinationfilename.pyo','-oo', doraise=True )

I have tried with python 2.7 and it only seems to work when you pass on all the parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM