- Python Snacks
- Posts
- The purpose of __pycache__ directory
The purpose of __pycache__ directory
And here's why you don't need to touch it (for the most part)

You may have seen a mysterious directory appear in your folder called __pycache__. It’s a folder that contains .pyc files, which stands for Python Compiled file.
Here, I’m going to take a small dive into what this folder (and these files) are, and why you generally don’t need to touch this folder and its contents.
Article Contents
What is the __pycache__ directory?
The __pycache__ directory is a special directory where Python’s bytecode is placed. This directory is managed by Python and appears in the same directory as your Python scripts.
![]() Discord bot __pycache__ directory | Take my discord bot, for instance. If we open this directory we see 4 files:
|
All of these files are not human-readable scripts, but are instead compiled to bytecode that Python interprets to execute your code.
The creation of these files speeds up program start time by saving the interpreter the effort of recompiling source files each time the program runs.
Benefits of the __pycache__ directory
Python creates this directory for a few different reasons, all of which benefit the interpreter:
Improved execution speed: As mentioned above, Python doesn’t need to re-compile each time you run the code.
Isolation of code: By keeping the bytecode separate from your source code, it leads to a clean(er) project directory structure. This is particularly useful when it comes to version control, as all you’d need to do is add the __pycache__ directory to your .gitignore file.
Best Practices and Considerations
Remember that this directory is handled by Python directly, so any manual intervention isn’t necessary. However, there may be a few times where you may need to tinker with this directory:
Clearing the cache: Sometimes, your program will do really weird things and there isn’t a clear cut reason as to why. By deleting this directory and re-running it, it’ll re-compile the code to the most up-to-date version.
Version control: Be sure to not check this directory into source control. Since these files are dependent upon your system and Python version, these files will differ between environments, etc.
📧 Join the Python Snacks Newsletter! 🐍
Want even more Python-related content that’s useful? Here’s 3 reasons why you should subscribe the Python Snacks newsletter:
Get Ahead in Python with bite-sized Python tips and tricks delivered straight to your inbox, like the one above.
Exclusive Subscriber Perks: Receive a curated selection of up to 6 high-impact Python resources, tips, and exclusive insights with each email.
Get Smarter with Python in under 5 minutes. Your next Python breakthrough could just an email away.
You can unsubscribe at any time.
Interested in starting a newsletter or a blog?
Do you have a wealth of knowledge and insights to share with the world? Starting your own newsletter or blog is an excellent way to establish yourself as an authority in your field, connect with a like-minded community, and open up new opportunities.
If TikTok, Twitter, Facebook, or other social media platforms were to get banned, you’d lose all your followers. This is why you should start a newsletter: you own your audience.
This article may contain affiliate links. Affiliate links come at no cost to you and support the costs of this blog. Should you purchase a product/service from an affiliate link, it will come at no additional cost to you.
Reply