7

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?

Ram Rachum
  • 84,019
  • 84
  • 236
  • 374

4 Answers4

10

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

In your code, spawn (e.g., 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.

Emil Sit
  • 22,894
  • 7
  • 53
  • 75
  • 6
    Note that the correct way to invoke the `compileall` module is using `python -m compileall`, NOT `python /path/to/compileall.py`. And figure out the location of the Python interpreter via `sys.executable` – intgr Jun 30 '12 at 00:40
2

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

Alexander Gessler
  • 45,603
  • 7
  • 82
  • 122
1

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.

Zaur Nasibov
  • 22,280
  • 12
  • 56
  • 83
0

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.

Markus W
  • 1,451
  • 5
  • 19
  • 32