Python generates Pyc files when executing Python (.py) scripts for the first time. You can find it in the Pycache folder. Here’s a path to check the location of it and Pyc files.

Pycache Folder in Python
Photo by Huy Phan on Pexels.com

Table of contents

  1. Pyc files in Python
  2. Location of Pycache folder

Pyc files in Python

The folder where your Python code you run, there, you can find a folder with the name __Pycache__. This folder is now a storage location for Pyc files. Below example shows, how to generate Pyc files.

~$ vi sample1.py
~$ chmod 777 sample1.py
~$ ./sample1.py
I am inside of the script
~$

In the same location, here’s the Pycache folder.

Location of Pycache folder

~$ ls
2023-11-02-file-1.ipynb 2024-01-04-file-2.term day.csv sample.py sample1.json sample2.sh test.py
2024-01-04-file-1.ipynb __pycache__ sample.db sample.sh sample1.py test.json test1.py
~$

Enter the cd command to move to the Pycache folder.

~$ cd __pycache__ 
~/__pycache__$ ls
sample.cpython-310.pyc test.cpython-310.pyc test1.cpython-310.pyc
~/__pycache__$
Here, sample1.pyc not created. The reason is we have not compiled, we just executed.

After issuing the compile command the Pyc file generated for sample1.py.

~$ python -m compileall sample1.py
~$ cd __pycache__
~/__pycache__$ ls
sample.cpython-310.pyc sample1.cpython-310.pyc test.cpython-310.pyc test1.cpython-310.pyc
~/__pycache__$