57

Is there a way to compile a Python .py file from the command-line without executing it?

I am working with an application that stores its python extensions in a non-standard path with limited permissions and I'd like to compile the files during installation. I don't need the overhead of Distutils.

5
  • What's wrong with just storing the .py files? Compilation is completely optional. Commented Jan 11, 2010 at 14:39
  • 1
    also distutils is hardly overhead Commented Jan 11, 2010 at 14:40
  • 5
    @nosklo: it's called faster start-up time. Commented Jan 11, 2010 at 14:41
  • @nosklo: when packaging for a debian repo (like what I am doing in this instance), if I can save from having to worry about distutils, I try to. Commented Jan 11, 2010 at 14:43
  • A similar question: stackoverflow.com/questions/5607283/… Commented Oct 30, 2017 at 11:39

6 Answers 6

87

The py_compile module provides a function to generate a byte-code file from a source file, and another function used when the module source file is invoked as a script.

python -m py_compile fileA.py fileB.py fileC.py
Sign up to request clarification or add additional context in comments.

2 Comments

I can’t find where the compiled file goes to.
output file goes to pycache dir
21

Yes, there is module compileall. Here's an example that compiles all the .py files in a directory (but not sub-directories):

python -m compileall -l myDirectory

Comments

5

In fact if you're on Linux you may already have a /usr/bin/py_compilefiles command in your PATH. It wraps the the py_compile module mentioned by other people. If you're not on Linux, here's the script code.

2 Comments

+1 for the interesting info but I'd rather not rely on this script being present (or not).
The link is broken. If it's possible to fix, please fix.
4

$ python -c "import py_compile; py_compile.compile('yourfile.py')"

or

$ python -c "import py_compile; py_compile.compileall('dir')"

1 Comment

hmmm... the solution proposed by @Roger Pate appears cleanly but thanks.
2

In addition to choose the output location of pyc (by @Jensen Taylor's answer), you can also specify a source file name you like for traceback if you don't want the absolute path of py file to be written in the pyc:

python -c "import py_compile; py_compile.compile('src.py', 'dest.pyc', 'whatever_you_like')"

Though "compileall -d destdir" can do the trick too, it will limit your working directory sometimes. For example, if you want source file name in pyc to be "./src.py", you have to move working directory to the folder of src.py, which is undesirable in some cases, then run something like "python -m compileall -d ./ ."

Comments

1

I would say something like this, so you can compile it to your chosen location:

import py_compile
py_compile(filename+".py",wantedlocation+wantedname+".pyc")

As I have now done in my Python project on github.com/lolexorg/Lolex-Tools

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.