tags:

views:

143

answers:

3

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

http://stackoverflow.com/questions/2998215/if-python-is-interpreted-what-are-pyc-files

http://stackoverflow.com/questions/3878479/python-pyc-files-main-file-not-compiled

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.

+2  A: 

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
Thank you @meder
J3M 7OR3
+3  A: 

There is a discussion on SO related to this area:

pyfunc
@pyfunc: When a question is a duplicate, please vote to close it, providing this question as the one being duplicated.
S.Lott
@S.Lott: Sure and this would be my first vote for closure as duplicate. I was shying away from doing so.
pyfunc
@pyfunc: Don't shy away from firsts. Do your research. Stick to your guns when you find something. If you shy away from "firsts", you can never make any progress at all.
S.Lott
@S.Lott actually the question is not a duplicate but some of the questions in the post are similar. also the answers are duplicate. I would like to say that I did look this up on Stack Overflow before submitting this question and did not find that post. try searching 'difference between .py and .pyc' in Stack Overflow and you will see @pyfunc suggested SO question does not even show up. Also look at the question itself that @pyfunc suggested. It says 'Python pyc files (main file not compiled?'. That certainly does not equate to 'Whats the difference between .py and .pyc files?'.
J3M 7OR3
@J3M 7OR3: "That certainly does not equate" If that's your test -- literal equality -- then you're doomed to ask a lot of duplication question through simple word-order changes or spelling mistakes. If you found other questions that were related, then *list them in your question*. Please provide some evidence for your background in your question. We can't guess at what you know and what you don't know and what you've already read. And "thinking" is more-or-less essential. "equate" may not be good enough.
S.Lott
@S.Lott I have to say that @pyfunc was wise to not vote for closure because @pyfunc understood it is not a duplicate question but that it was in fact related. you spoke too soon and I doubt you did your research. @pyfunc, thanks for the link to the other related Stack Overflow question. It was very informative.
J3M 7OR3
@J3M 7OR3: "wise to not vote for closure". Facts please? How do you know who actually voted for closure. Also, please **update** your question with some actual reason why it's not a duplicate. Merely repeating your claim isn't really evidence, is it? Provide some evidence, please. Read the duplicate question and then demonstrate that yours is somehow different. It's your question. If you want an answer, provide us a reason to answer it.
S.Lott
@S.Lott The question titles 'Python pyc files (main file not compiled?' and 'Whats the difference between .py and .pyc files?' do not equate to each other. I should correct my first comment. When I ran a SO search for 'difference between .py and .pyc', I actually did see the 'Python pyc files (main file not compiled?)' in the list of questions returned but I never looked at it. I can not go through every answer that comes back especially when the title leads me to believe the answer will not be contained with in the post. My question's title however is clear and straight to the point.
J3M 7OR3
"I can not go through every answer that comes back" Ahhh. I get it. You (1) can ask for help and then (2) refuse to clarify that plea for help. Does that mean you can (3) complain when no one provides the help you hoped for after you refused to clarify? I'll make this clear. It actually **is** your obligation to prove your question is unique. We can close it. Only you can provide evidence to prevent closure. Let me make this clear. If you don't provide evidence that it's unique, it **will** get closed, possibly with no useful answer. Do you want help? Fix your question.
S.Lott
@S.Lott This will help others, especially beginners like myself, find this bit of information much easier now. There is quite a bit of value in my question and it is not a duplicate. The fact that there are other questions on SO that contain the answer to my question does not make my question a duplicate. @pyfunc said related and you took that as meaning duplicate. there is a fine line between the two. i hope you can see this.
J3M 7OR3
@S.Lott if you would have read my question then you would have clearly seen that it is not a duplicate of any other question. However I will admit that my question is related to many other questions. Then again I understand why you would think it might be a duplicate. I don't think you looked hard enough though.
J3M 7OR3
@J3M 7OR3: "you would have clearly seen" Sadly, I didn't clearly see any such thing. Continue to make stuff up about other people. It's helpful. "I don't think you looked hard enough". You're entitled to that opinion. I'll try to make this clear. **Don't Blame Me For Trying To Help**. If you want help, **Fix Your Question**. I don't think I can make it more clear. Please don't make things up about what is perfectly clear to other people. There's a small possibility that your question wasn't totally perfect. Consider that possibility, please.
S.Lott
@J3M 7OR3: @S.Lott: Can we possibly close this? It is already looking bad among the community. We are here to learn while asking and answering the questions. I am not one to preach. I have avoided participating in the chain of comments. Both of you have a position and is valid. Let it be at that. It is not worth it. Enjoy your moments at stackoverflow. At the same time, people who feel this is a duplicate will vote for closure. This is community and comes with it's constraints. Lets do our best, irrespective of our position.
pyfunc
@pyfunc: The point is this. And I emphasize because there is a problem with J3M 7OR3's claim. "you would have clearly seen" is something no one should ever say about another human being. Period.
S.Lott
@S.Lott All I meant was that my question was another way to get to the same information. Not everyone uses the same path to get to the same goal. That is all. I just thought that the difference between the two question titles were clear to most everyone. Perhaps I assumed too much. To me 'What are .pyc files?' and 'What is the difference between .py and .pyc files?' are quite different. So what ever happens will happen. I just think this question will help others find the info easier. I hope it stays open. @pyfunc I hear you. I'm done.
J3M 7OR3
@J3M 7OR3: "Perhaps I assumed too much". Not only that, but you continued to insist that your question was perfect and you did not need to demonstrate that your question was in any way unique. Please think about *other people* who see the question, *think* and say 'What are .pyc files?' and 'What is the difference between .py and .pyc files?' are identical. Please *think* about people who's depth of understand is not as perfect as yours. Please *think* about those poor sods -- like me -- and fix your question **without discussion**. When asked, just fix the question and move on. Please.
S.Lott
+4  A: 

.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
@mipadi: "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."
singularity
@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
@mipadi: yes, maybe i miss understood when you said "save some time when running programs", i was thinking that you was talking about running time, because what can be saved is in loading time, but basically as you said it will save time let say "in general" :) .
singularity