1

I don't remember these files being there when I first started working with my Django application, and they are not mentioned in the Django tutorial's list of files

I looked into this more and discovered that these are what is known as bytecode – as discussed at this S.O. question

How did these files get created, and should I keep them in my folder or trash them?

Community
  • 1
  • 1
Drewdavid
  • 3,071
  • 7
  • 29
  • 53

1 Answers1

5

Bytecode files are created by the interpreter the first time the code is run, so that (among other things) it will run faster with subsequent running. The only reason you should remove them is if you've changed the code in one of the corresponding .py files, and for some reason the interpreter is not picking up the changes (possibly due to an issue with the timestamp). Additionally, you can also get rid of them if you change versions of Python (for example, from 2 to 3, or from 2.6 to 2.7). Changing micro versions (such as 2.7.3 to 2.7.9) won't affect the bytecode structure, so a small upgrade such as that is harmless. And, as sapi points out, if you want to delete a .py file for some reason, you should also delete the corresponding .pyc as well.

From mgilson, see this blog post from Ned Batchelder on the structure and function of .pyc files.

Community
  • 1
  • 1
MattDMo
  • 100,794
  • 21
  • 241
  • 231
  • 2
    Even then, python will recognize the .py file is newer than the .pyc and it'll re-generate the .pyc... Of course, if you go to the OS level and start messing with timestamps, then you might want to consider deleting them :-) – mgilson Jan 06 '15 at 21:13
  • @mgilson is that true for all python versions? I'm pretty sure I can remember cases where older bytecode was run instead of a newer source code file - I think it was with Py2. – MattDMo Jan 06 '15 at 21:15
  • Thanks ok, so I should get used to seeing these .pyc files in there then...? – Drewdavid Jan 06 '15 at 21:41
  • @Drewdavid yup, don't worry about them. If mgilson is correct for your version of Python, they should be automatically regenerated. To test this, try making a change in one of the files, then see if it's reflected on your site (you may need to restart the server, but try without it first). – MattDMo Jan 06 '15 at 21:43
  • If it wasn't true for some weird python version, then that would make developing/testing that particular code really annoying/inconvenient. I find it hard to believe that it ever behaved differently ... – mgilson Jan 06 '15 at 22:35
  • 1
    And a neat [blog post](http://nedbatchelder.com/blog/200804/the_structure_of_pyc_files.html) by Ned Batchelder ... – mgilson Jan 06 '15 at 22:38
  • @mgilson thanks for that link, it's really interesting. I updated my answer to clarify when `.pyc` files should be deleted. – MattDMo Jan 07 '15 at 00:48
  • The one time you probably do want to delete the `.pyc` files is if you've removed the corresponding `.py` files. That ensures that all imports etc using the old names will fail. – sapi Jan 07 '15 at 00:55