134

I have noticed .pyc files spontaneously being generated when some .py file of the same name gets run. What is the difference between .py and .pyc files?

Also, I find that having .pyc files lying around clutters up space. Should one delete .pyc files? Or is there a benefit and/or necessity to having them around?

UPDATE: Here are 2 answered questions that are related to my question

If Python is interpreted, what are .pyc files?

Why are main runnable Python scripts not compiled to pyc files like modules?

This Question is not a Duplicate

Reason 1: Because I am asking what the difference between these two files are. The question S.Lott found named 'If Python is interpreted, what are .pyc files?' is not asking what the difference between .py and .pyc files are. It is asking what .pyc files are.

Reason 2: Because my secondary questions 'Should one delete .pyc files? Or is there a benefit and/or necessity to having them around?' provide even more information on .pyc files and how one should handle them.

Reason 3: Because when a beginner Python programmer like myself wants to find out What is the difference between .py and .pyc files? , they will have no problem finding out the answer as they will be guided directly to my question. This helps reduce search time since the question is right to the point.

Community
  • 1
  • 1

3 Answers3

89

.pyc contain the compiled bytecode of Python source files. The Python interpreter loads .pyc files before .py files, so if they're present, it can save some time by not having to re-compile the Python source code. You can get rid of them if you want, but they don't cause problems, they're not big, and they may save some time when running programs.

mipadi
  • 398,885
  • 90
  • 523
  • 479
  • 4
    "they may save some time when running programs." you are wrong here, check the doc (http://docs.python.org/tutorial/modules.html) : "A program doesn’t run any faster when it is read from a .pyc or .pyo file than when it is read from a .py file; the only thing that’s faster about .pyc or .pyo files is the speed with which they are loaded." – mouad Oct 12 '10 at 22:33
  • 23
    @tzzzzz: True, but load time will affect total speed of execution, especially for smaller programs where a higher proportion of time is spent loading/compiling the source code. – mipadi Oct 12 '10 at 23:53
  • 2
    also, pyc works independently from py, once compiled to bytocode the py source is not needed – Vitaliy Terziev Jun 23 '16 at 10:42
18

Python compiles the .py and saves files as .pyc so it can reference them in subsequent invocations.

There's no harm in deleting them, but they will save compilation time if you're doing lots of processing.

meder omuraliev
  • 183,342
  • 71
  • 393
  • 434
12

A program doesn't run any faster when it is read from a ".pyc" or ".pyo" file than when it is read from a ".py" file; the only thing that's faster about ".pyc" or ".pyo" files is the speed with which they are loaded.

http://docs.python.org/release/1.5.1p1/tut/node43.html

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Name
  • 121
  • 1
  • 2