diff --git a/Lib/test/test_compileall.py b/Lib/test/test_compileall.py index e9caa82a700d2f..ab0be90c92ca2d 100644 --- a/Lib/test/test_compileall.py +++ b/Lib/test/test_compileall.py @@ -1154,10 +1154,16 @@ def test_import(self): # Change of the module content script = self.make_script("print(0)", name="module") - # Import the module in Python with -O (optimization level 1) - script_helper.assert_python_ok( - "-O", "-c", "import module", __isolated=False, PYTHONPATH=self.path - ) + # Import the module in Python with -O (optimization level 1). + # The child must write the opt-1 pyc file, so ignore any ambient + # PYTHONDONTWRITEBYTECODE or PYTHONOPTIMIZE. + with os_helper.EnvironmentVarGuard() as env: + env.unset('PYTHONDONTWRITEBYTECODE') + env.unset('PYTHONOPTIMIZE') + script_helper.assert_python_ok( + "-O", "-c", "import module", + __isolated=False, PYTHONPATH=self.path + ) # Only opt-1.pyc is changed self.assertEqual(inode, os.stat(pycs[0]).st_ino)