While touching python "That ...? Haven't you seen the pyc file lately?"
I noticed.
Then, the specifications have changed firmly (although it is quite old), so I wrote it.
Officially, this explanation is written.
CPython compiles its source code into "byte code", and for performance reasons, it caches this byte code on the file system whenever the source file has changes. This makes loading of Python modules much faster because the compilation phase can be bypassed. When your source file is foo.py, CPython caches the byte code in a foo.pyc file right next to the source.
https://www.python.org/dev/peps/pep-3147/
To summarize in 3 lines
--Compile the source code to be imported at any time to make it a computer-readable binary file! --Binary files load very quickly! --The binary file will be created as a pyc file right next to the file!
Is written. (Very rough)
Therefore, it plays a role in speeding up loading.
├─ a.py
├─ b.py
└─ c.py
a.py
import b
import c
b.hello()
c.goodmorning()
b.py
def hello():
print("hello")
c.py
def goodmorning():
print("goodmorning")
hello
goodmorning
$ ls
a.py b.py b.pyc c.py c.pyc
pyc file has been Hello.
But sometime, this pyc file wasn't created right away.
Clearly
I thought, but that was not the case.
It seems that the storage location has changed from python3.2.
To prevent these new files from cluttering the source directory, pyc files are now collected in the "pycache" directory under the package directory.
https://docs.python.org/ja/3/whatsnew/3.2.html#pep-3147-pyc-repository-directories
Hmmm, surely it was double and hard to find the source code.
So you changed the management method.
This time it has the same file structure, but confirmed with python3.2 (I tried it with 2.7 above)
├─ a.py
├─ b.py
└─ c.py
a.py
import b
import c
b.hello()
c.goodmorning()
b.py
def hello():
print("hello")
c.py
def goodmorning():
print("goodmorning")
$ ls
__pycache__ a.py b.py c.py
$ ls __pycache__/
b.cpython-37.pyc c.cpython-37.pyc
I was able to confirm that .pyc was created under pycache / like this!
Recommended Posts