r/learnpython • • May 19 '17

Sometimes I see Python creates __pycache__ folder, sometimes not. Why?

Recently I've noticed that a new folder appeared in my coding project. Its name is __pycache__. I have never noticed anything like that.

Why does Python create it? Does it have any special meaning? Why doesn't Python create it whenever I write a new script? How does it work?

Thank you very much.

9 Upvotes

4 comments sorted by

View all comments

2

u/michalesco May 19 '17

Thanx a lot! Correct me if I am wrong: if I delete the folder, nothing bad happens at it would be created again when the scripts import a module(s) again. Right?

One more question: The folder appeared when I imported my own module, I haven't seen it before. Does it mean that when I import a module from Python standard library, the pycache folder is not created? Is this something like a rule?

4

u/K900_ SRE person // please don't DM me your questions May 19 '17

If you delete the folder, it will just be recreated. No such folder is created for the standard library, as the standard library is pre-cached when you install Python. Also, if you want to reply to a comment, click the reply link under that comment, not at the top level ;)

2

u/zahlman zahlman May 19 '17

No such folder is created for the standard library, as the standard library is pre-cached when you install Python.

  1. I could have sworn that the pre-caching is optional (but default).

  2. There is such a folder - it's just inside the install directory :)