Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VS Code Keeps Generating Pycache Files

I already added the following snippet to the top of my python codes to prevent pycache files from being generated.

import sys
sys.dont_write_bytecode = True

Now, if I run the files (unit tests) individually, no pycache file is generated. However, as soon as I use the Testing feature of VS Code to run all unit tests one after another, it consistently generates a pycache folder.

Thanks in advance for any help!

like image 289
Nguyen Tran Avatar asked Apr 15 '26 16:04

Nguyen Tran


1 Answers

Reasons for Pycache folder generation

When B.py import A.py, there will generate a A.pyc

How to avoid .pyc files

  1. python3 -B test.py Use this command run the .py file.
  2. add the following code into the .py file:

import sys sys.dont_write_bytecode = True

  1. Setting environment variables

    PYTHONDONTWRITEBYTECODE=1

enter image description here

According to your situation, I suggest you tring the third way to avoid the .pyc file.

like image 137
MingJie-MSFT Avatar answered Apr 18 '26 06:04

MingJie-MSFT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!