0

I feel like I'm being really dumb with this. I know I can compile a .py file to .pyc with python -m compileall with the logic that the script will load faster because it's pre-compiled, but that doesn't seem to be a useable feature if it's the primary script. To explain what I mean, lets say I run test.py from command line like so:

python test.py

Now let's assume test.py is a large file that takes some time to convert to byte code, the logical thing to do is precompile it, so I run the following in command:

python -m compileall test.py

Then I go and run the script again using:

python test.py

If I understand correctly, the test.py file is recompiled, basically making the pre-compile pointless. Am I getting this wrong? If I'm not, is there a way around this?

3
  • I believe this thread might answer your question: stackoverflow.com/questions/471191/why-compile-python-code Commented Jan 20, 2020 at 2:50
  • @RubenBrekelmans I've read that thread. It touches on the issue, but it's not clear (at least not to me). Commented Jan 20, 2020 at 3:05
  • At the risk of repeating the other thread, but the way I understand it, is that in your case of running a single file with no additional imports, compiling would not make a difference. Were you to import modules in your test.py, than compiling would affect those imports but not test.py itself. Why it recompiles test.py, I am afraid I don't know. Commented Jan 20, 2020 at 4:31

1 Answer 1

0

Pre-compile generates a .pyc file in ./__pycache__. By running the following command, you can get it:

$ python3 -m compileall py.py
Compiling 'py.py'...
$ ls __pycache__
py.cpython-38.pyc
$ # It exists

To test if running the script recompiles everything, I removed it.

$ rm -fr __pycache__
$ python3 py.py
Helloworld
$ ls __pycache__
ls: __pycache__: No such file or directory

As you saw, scripts aren't recompiled by python script.py(it just execute it.), different from python -m compileall script.py.

If I understand correctly, the test.py file is recompiled, basically making the pre-compile pointless.

So it isn't pointless.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! But this leaves me with the question, does python check for the compiled file, and use that if it exists? Eg. If you didn't delete the compiled version before you ran the file, would the compiled version have been used?
While running python path/to/projectDir or importing a module (a single file is ok.), it will use pyc. But While running a single file such as python path/to/projectDir/__main__.py, it won't. @user6916458

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.