Why does Python compile libraries that are used in a script, but not the script being called itself?
For instance,
If there is main.py and module.py, and Python is run by doing python main.py, there will be a compiled file module.pyc but not one for main. Why?
-
If the response is potential disk permissions for the directory of
main.py, why does Python compile modules? They are just as likely (if not more likely) to appear in a location where the user does not have write access. Python could compilemainif it is writable, or alternatively in another directory. -
If the reason is that benefits will be minimal, consider the situation when the script will be used a large number of times (such as in a CGI application).
Files are compiled upon import. It isn’t a security thing. It is simply that if you import it python saves the output. See this post by Fredrik Lundh on Effbot.
When running a script python will not use the *.pyc file.
If you have some other reason you want your script pre-compiled you can use the
compileallmodule.compileall Usage
Modules and scripts are treated the same. Importing is what triggers the output to be saved.
Using compileall does not solve this. Scripts executed by python will not use the
*.pycunless explicitly called. This has negative side effects, well stated by Glenn Maynard in his answer.The example given of a CGI application should really be addressed by using a technique like FastCGI. If you want to eliminate the overhead of compiling your script you may want eliminate the overhead of starting up python too, not to mention database connection overhead.
A light bootstrap script can be used or even
python -c "import script", but these have questionable style.