Ablaufschema: Eine .py-Datei wird von Python geprüft. Existiert eine passende .pyc-Datei im Ordner __pycache__, wird sie direkt ausgeführt. Sonst wird der Programmtext in Bytecode übersetzt, als .pyc gespeichert und dann ausgeführt.

Python Cache

The Python cache is a cache in which the programming language Python stores results it has already computed, so it doesn't have to redo them next time. This usually refers to the automatically created files in the __pycache__ folder, but sometimes also to custom-built caches within a program.

Python is a widely used programming language. It is especially often used for data analysis and for programs in the field of artificial intelligence. A cache is, quite generally, a temporary store: it holds a result that has already been computed once. The next time, the program simply retrieves the result instead of computing it anew. The term Python cache actually refers to two things that should be kept apart. First, Python itself automatically stores translated program files in a folder called __pycache__. Second, developers build caches into their own programs so that expensive computation steps don’t have to be repeated constantly.

How much computing time the cache saves

Computing time costs money and electricity. This is especially true where the same task occurs millions of times. A cache is the simplest way to get rid of these repetitions. It doesn’t make a program smarter, but it often makes it many times faster.

An everyday example from developers: A program queries the same exchange rate from a remote server for every user. Each query might take 200 milliseconds. If the rate is cached for one minute, the number of queries drops from thousands to just one. The server on the other end is relieved, and the application itself responds noticeably faster.

Python’s own automatic cache also saves measurable time. Without it, Python would have to retranslate every file each time a program starts. In large projects with hundreds of files, this turns a fast startup into a noticeable wait. This is precisely why the mechanism is enabled by default.

From source file to .pyc file

The program text a human writes is stored in files with the .py extension. The computer cannot execute this text directly. Python therefore first translates it into a more compact intermediate form called bytecode. Python stores this bytecode as a file with the .pyc extension in the __pycache__ folder.

At the next startup, Python checks whether the original file has changed. To do this, it compares the timestamp and the file size. If everything matches, the finished .pyc file is used and the translation step is skipped. If the program text has changed, it is retranslated and the cache is updated. So you normally never need to touch this folder.

The second case, the custom-built cache, works differently. Here the program stores pairs of question and answer in a table in working memory. If the same question comes up again, the stored answer is returned. Python comes with a ready-made tool for this, which can be applied with a single line via a function decorator. What matters is the question of when an entry becomes invalid: an exchange rate from yesterday is simply wrong. This problem is called cache invalidation and is considered one of the classic hard problems in software development.

Where the term shows up in projects and news

Anyone who works with Python themselves stumbles early on across the __pycache__ folder. It appears on its own next to your own files and regularly puzzles beginners. In practice, it is excluded from version control, i.e., from the system that tracks all changes to the program text. Its contents can be regenerated at any time and therefore do not belong in the project archive.

In news about AI systems, it is usually the second meaning that is meant. Providers of language models, for instance, advertise a cache for recurring text blocks. If the same long instruction text is sent along again and again, it doesn’t have to be processed anew each time. This significantly lowers costs and response times, and pricing lists list cached requests separately.

A common misconception is to regard the Python cache as a source of errors. Outdated .pyc files cause practically no problems today, because the check works reliably. If a program behaves strangely, it is almost always due to one’s own code. The advice to just delete the cache therefore helps less often than many forum posts claim.

Subscribe free. Unsubscribe the second it sucks.

High-signal news across AI, business, UX, and tech. Every morning.