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?

user6916458
  • 400
  • 2
  • 7
  • 19
  • I believe this thread might answer your question: https://stackoverflow.com/questions/471191/why-compile-python-code – RubenB Jan 20 '20 at 02:50
  • @RubenBrekelmans I've read that thread. It touches on the issue, but it's not clear (at least not to me). – user6916458 Jan 20 '20 at 03: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. – RubenB Jan 20 '20 at 04:31

1 Answers1

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.

xiaoyu2006
  • 507
  • 7
  • 20
  • 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? – user6916458 Jan 20 '20 at 03:04
  • 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 – xiaoyu2006 Jan 20 '20 at 03:09