Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Setting the PYTHONOPTIMIZE Environment Variable

00:00 Setting the PYTHONOPTIMIZE Environment Variable. You can also run Python in optimized mode with disabled assertions by setting the PYTHONOPTIMIZE environment variable to an appropriate value.

00:13 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.

00:31 If you’re using the Windows command prompt, then use this command.

00:41 If you’re using Windows Terminal, then use this command. And use this command for macOS and Linux.

01:02 Once you’ve set PYTHONOPTIMIZE to a non-empty string, you can launch your Python interpreter with the bare-bones python command.

01:09 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 circle.py file.

01:53 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 -O option n times.

02:14 In other words, you are using n levels of optimization. On-screen are the two settings for the Windows command prompt

02:33 for Windows terminal,

02:45 and here they are for MacOS and Linux. In each case, 1 is equivalent to python O, and 2 is equivalent to python -OO.

03:03 You can use any integer number to set PYTHONOPTIMIZE, but Python only implements two levels of optimization. Using a number greater than 2 has no real effect on your compiled bytecode.

03:15 Setting PYTHONOPTIMIZE to 0 will cause the interpreter to return to normal mode. When you run Python, the interpreter compiles any imported module to bytecode on the fly.

03:29 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.

03:46 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.

04:04 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 .pyc files.

04:22 In the next section of the course, you’ll see how to test your code with assertions.

Become a Member to join the conversation.