You can use the py_compile module. Run it from the command line ( -m ):
When this module is run as a script, main () is used to compile all the files whose names are specified on the command line.
Example:
$ tree . βββ script.py 0 directories, 1 file $ python3 -mpy_compile script.py $ tree . βββ __pycache__ β βββ script.cpython-34.pyc βββ script.py 1 directory, 2 files
compileall provides similar functionality, to use it, you would do something like
$ python3 -m compileall ...
Where ... - files for compilation or directories containing source files are moved recursively.
Another option is to import the module:
$ tree . βββ module.py βββ __pycache__ β βββ script.cpython-34.pyc βββ script.py 1 directory, 3 files $ python3 -c 'import module' $ tree . βββ module.py βββ __pycache__ β βββ module.cpython-34.pyc β βββ script.cpython-34.pyc βββ script.py 1 directory, 4 files
-c 'import module' is different from -m module because the first will not execute the if __name__ == '__main__': block in module.py.
vaultah
source share