Setting the PYTHONOPTIMIZE Environment Variable
For example, setting this variable to a non-empty string is equivalent to running Python with the previously seen
-O option. To try
PYTHONOPTIMIZE out, fire up your command line and run one of the following commands, depending on the operating system and shell that you are using.
This command will automatically run Python in optimized mode. This holds true for the bpython interpreter seen on-screen as well. Now go ahead and run the code from the directory containing the
Assertions are off, and the
Circle class accepts negative radius values. You are running Python in optimized mode once more. Another possibility is to set
PYTHONOPTIMIZE to an integer value,
n, which is equivalent to running Python using the
The compiled bytecode will live in a directory called
__pycache__/, which is placed in the directory containing the module that provided the imported code. Inside
__pycache__/, you’ll see a
.pyc file named after your original module plus the interpreter’s name and version.
The name of the
.pyc file will also include the optimization level used to compile the code. For example, when you import code from
circle.py, the Python 3.11 interpreter generates the file seen on-screen, depending on the optimization level.
The name of each file includes the original module’s name, the interpreter that generated the code, and the optimization level. PEP 488 provides more context on this naming format for
Become a Member to join the conversation.