Compile python code then place it somewhere else
Question: I have python file, for example, named blah.py, that I would like to have
compiled then placed into another folder using cmake. Right now, I am capable
of doing this with the following code in my cmake file:

    
    
    ADD_CUSTOM_TARGET(output ALL /usr/bin/python -m py_compile src/blah.py
                      COMMAND /bin/mv src/blah.pyc build VERBATIM)
    

This is on ubuntu 12.04. This code works as intended; the only problem is that
the python file is being compiled in the source directory, then being put in
the build directory.

However, I can't assume that this src directory will have read AND write
privileges, meaning what I need to do is combine these two commands into one
(compile the python file and place the compiled python file into my build
directory, instead of compiling it in the src directory then moving it)

I'm sure there must be some way I could be using to specify where I would like
this compiled code to be placed, but I can't find any. Help would be greatly
appreciated! :)

EDIT: This link may have a solution..not sure:

[Can compiled bytecode files (.pyc) get generated in different
directory?](http://stackoverflow.com/questions/611967/can-compiled-bytecode-
files-pyc-get-generated-in-different-directory)


Answer: I was typing out this answer, and then looked at your edited link. This same
answer is given in one of the unaccepted answers:
<http://stackoverflow.com/a/611995/496445>

    
    
    import py_compile
    py_compile.compile('/path/to/source/code.py', cfile='/path/to/build/code.pyc')
    

To call this via a basic shell command you can format it like this:

    
    
    python -c "import py_compile; py_compile.compile('/path/to/source/code.py', cfile='/path/to/build/code.pyc')"
    

