pythonpython-3.xcompilationpython-3.5bytecode

Choose location of compiled file


So I have a program that generates the bytecode of a python file. In this case i want to import statements from the bytecode(.pyc) file. I believe the file has to be in a parent folder so lets just say I want the file once compiled to be stored at /root/users/folder. But the code here only allows me just to compile it and store it into the current folder.

Code:

from py_compile import compile as bytecode

bytecode(file='file.py')

Now I know a could just just move the file using another function but is their another way to store it specifically like how modules are stored in /root/users/__pycache__ when it is imported.


Solution

  • py_compile.compile accepts an optional parameter cfile:

    import py_compile
    
    py_compile.compile('file.py', cfile='/path/where-you-want-to-save/file.py')