Why does the existence of .pyc file change the result of my code?

1.7k views Asked by At

I have a test case for an algorithm, which gives a different result after the first execution.

The test imports the algorithm and the test data from two files.

The first execution returns the correct results and creates a .pyc file for the test data file. The second and all following executions return incorrect results. When I delete the test data's .pyc file the next execution returns the correct results again (and creates a new .pyc file again).

When I move the test data into the same file as the test case itself (i.e. avoiding the creation of a .pyc file) the test always passes.

I cannot apply this fix to my full program.

Is this a known issue, is there a fix?

2

There are 2 answers

0
iFlo On

.pyc files contain byte code, which is what the Python interpreter compiles the source to. This code is then executed by Python's virtual machine.

Python's documentation explains the definition like this:

Python is an interpreted language, as opposed to a compiled one, though the distinction can be blurry because of the presence of the bytecode compiler. This means that source files can be run directly without explicitly creating an executable which is then run

The .pyc files are created (and possibly overwritten) only when that python file is imported by some other script. If the import is called, Python checks to see if the .pyc file's internal timestamp matches the corresponding .py file. If it does, it loads the .pyc; if it does not or if the .pyc does not yet exist, Python compiles the .py file into a .pyc and loads it.

0
gerardw On

One thing I found that changes is the value of file (.pyc vs .py), which tripped me up writing a utility invoking stack traces.