Devarapalli Vamsi
Devarapalli Vamsi

Reputation: 55

What is the use of __pycache__ bytecode version of my file when the file itself is deleted?

I am playing around with import mechanism. Here's what happened:

dir/b.py -> Prints "hello"
dir/a.py --> Imports b.py

For the first time, when I imported b (from a.py), a directory named `_pycache_` popped up containing the bytecode version of b.py

Now, I have deleted and tried to run . I got the error saying that Python couldn't find a module named 'b', which makes sense. But why is the cached version still present (even after b.py is deleted)? I know the purpose of pycache, but I wonder if it doesn't add any memory overhead when there are, say, 20 files that are deleted but their cached versions continue to exist.

One more thing I have done is that I had explicitly added the __pycache__ directory to sys.path as follows:
enter image description here
Still, python is unable to do the import. Please help me understand the purpose of these cached files and please point out if I have misunderstood anything.

Upvotes: 0

Views: 30

Answers (0)

Related Questions