Python3 __pycache__ generating even if PYTHONDONTWRITEBYTECODE=1
Question:
I would like to ask if there is another way to disable creation of __pycache__
in the server.
My problem is it keeps generating even if I already set environment variable to PYTHONDONTWRITEBYTECODE=1
I want to disable __pycache__
because I only keep 5 releases in deployment. This cache is preventing the deletion of the 6th release because the cache is owned by root and can only be deleted through sudo.
I am using capistrano for deployment.
Thank you for your response!
Answers:
I already resolved this issue. export PYTHONDONTWRITEBYTECODE=1
works. The generation of pycache
folder is in the docker (I used docker as well). What I did is inside the docker, I have this export PYTHONDONTWRITEBYTECODE=1
So it solved the issue. thank you for your help.
You can also disable cache usage in the Python code via sys.dont_write_bytecode:
import sys
sys.dont_write_bytecode = True
import first_module
from second_module import MyClass
...
Important: You have to put these lines before each other import of your modules, because the creation of the cache file is triggered by the import statement.
For Visual Studio Code user
If the setting: python.testing.pytestEnabled
is true, Visual Studio Code seems to ignore PYTHONDONTWRITEBYTECODE=1
and generates __pycache__
at some time such as saving file.
I would like to ask if there is another way to disable creation of __pycache__
in the server.
My problem is it keeps generating even if I already set environment variable to PYTHONDONTWRITEBYTECODE=1
I want to disable __pycache__
because I only keep 5 releases in deployment. This cache is preventing the deletion of the 6th release because the cache is owned by root and can only be deleted through sudo.
I am using capistrano for deployment.
Thank you for your response!
I already resolved this issue. export PYTHONDONTWRITEBYTECODE=1
works. The generation of pycache
folder is in the docker (I used docker as well). What I did is inside the docker, I have this export PYTHONDONTWRITEBYTECODE=1
So it solved the issue. thank you for your help.
You can also disable cache usage in the Python code via sys.dont_write_bytecode:
import sys
sys.dont_write_bytecode = True
import first_module
from second_module import MyClass
...
Important: You have to put these lines before each other import of your modules, because the creation of the cache file is triggered by the import statement.
For Visual Studio Code user
If the setting: python.testing.pytestEnabled
is true, Visual Studio Code seems to ignore PYTHONDONTWRITEBYTECODE=1
and generates __pycache__
at some time such as saving file.