Home > Blockchain >  Compiling python files using py_compile in command line
Compiling python files using py_compile in command line

Time:06-16

I am using the following command to compile a specific python script:

python3 -m py_compile abc.py

However I want this compiled file to reside in a different directory. I haven't been able to find anything over the internet regarding this. Is this even possible? So, I am trying something like:

python3 -m py_compile abc.py /Users/documents/abc.pyc

ofcourse the above command gives an error saying it can't find the file or directory /Users/documents/abc.pyc.

CodePudding user response:

I think it's impossible to specify named arguments with -m option. If -m is necessary, you can create a wrapper that runs py_compile and pass arguments with sys.argv.

$ cat compile_it.py

import sys
import py_compile
# cfile` is named argument for destination filename
py_compile.compile(sys.argv[1],  cfile=sys.argv[2])

And use it with: python -m compile_it abc.py /Users/documents/abc.pyc

Or without -m: python compile_it.py abc.py /Users/documents/abc.pyc

  • Related