If I thought I didn't see the pyc file recently, it was quarantined in pycache by python3.

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.

What is a pyc file?

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.

Let's create a pyc file!

 ├─ 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")

result

hello
goodmorning

$ ls
a.py		b.py		b.pyc		c.py		c.pyc

pyc file has been Hello.

The truth about the whereabouts of pyc

But sometime, this pyc file wasn't created right away.

Clearly image.png

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.

Check for new pyc files

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")

result

$ 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

If I thought I didn't see the pyc file recently, it was quarantined in pycache by python3.
Read the file line by line in Python
Read the file line by line in Python
The file name was bad in Python and I was addicted to import
In bash, "Delete the file if it exists".
The file name saved by pysheng was a hexadecimal number, so I fixed it.
When I checked the query generated by Django, it was issued in large numbers
I want to replace the variables in the python template file and mass-produce it in another file.
I wondered if Python 3.4 was faster, but it was slower
I was able to repeat it in Python: lambda
It was great to edit the Python file in the Raspberry Pi with Atom's remote function
[Python] Open the csv file in the folder specified by pandas
Download the file in Python
I thought about the logic that divides / 22 or / 23 into / 24 units by specifying the IP range in Python.
Check if the password hash generated by PHP matches in Python
Delete a particular character in Python if it is the last
Sort the file names obtained by Python glob in numerical order
Save the binary file in Python
I tried to open the latest data of the Excel file managed by date in the folder with Python
I wrote the queue in Python
I wrote the stack in Python
When I tried to run Python, it was skipped to the Microsoft Store
A memo organized by renaming the file names in the folder with python
I set the environment variable with Docker and displayed it in Python
If you are told cannot by Python import, review the file name
I made a program to check the size of a file in Python
I created a script to check if English is entered in the specified position of the JSON file in Python.
I thought it was the same as python, and I was addicted to the problem that the ruby interpreter did not start.
In IPython, when I tried to see the value, it was a generator, so I came up with it when I was frustrated.
Check if the URL exists in Python
I didn't know the basics of Python
[Python] Read the specified line in the file
The file edited with vim was readonly but I want to save it
Note that I understand the least squares algorithm. And I wrote it in Python.
Fourier transform the wav file read by Python, reverse transform it, and write it again.
[Python] None I made nullutil.py because it was cluttered by checking and branching.
I want to get the file name, line number, and function name in Python 3.4
I tried searching for files under the folder with Python by file name