2

I wrote a script, say, samplescript.py. All I can recall doing with, other than editing it, is running it through the command-line python interpreter.

Later, I found a samplescript.pyc file. Does running a script through the interpreter always invoke the compilation of the script?

3
  • 1
    Yes. It compiles it. How else should it know what to do? Commented Dec 22, 2013 at 15:21
  • The next time you run the script, your computer can use the .pyc file instead of re-compiling. A new .pyc file gets generated if the .py file changes. Commented Dec 22, 2013 at 15:22
  • stackoverflow.com/questions/2998215/… Commented Dec 22, 2013 at 15:24

1 Answer 1

5

When you execute your code, python creates a compiled pyc file. This file is the one executed in posterior runs if you do not modify your code

From here:

As an important speed-up of the start-up time for short programs that use a lot of standard modules, if a file called "spam.pyc" exists in the directory where "spam.py" is found, this is assumed to contain an already-``byte-compiled'' version of the module spam. The modification time of the version of "spam.py" used to create "spam.pyc" is recorded in "spam.pyc", and the file is ignored if these don't match.

Normally, you don't need to do anything to create the "spam.pyc" file. Whenever "spam.py" is successfully compiled, an attempt is made to write the compiled version to "spam.pyc". It is not an error if this attempt fails; if for any reason the file is not written completely, the resulting "spam.pyc" file will be recognized as invalid and thus ignored later. The contents of the "spam.pyc" file is platform independent, so a Python module directory can be shared by machines of different architectures.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.