Search code examples
pythonunit-testingtestingvisual-studio-codepyc

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!


Solution

  • 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.