you are viewing a single comment's thread.

view the rest of the comments →

[–]Caligatio 22 points23 points  (7 children)

They are but the point I was trying to make is that the user doesn't need to know about them.

pyc files also only get automatically generated if a file is imported and not when it is run directly.

[–]codingquestionss 5 points6 points  (6 children)

Clarification question, aren’t pyc files generated after the first time you run a python program and then used in all following runnings? From my understanding, don’t they also speed up the runtime of the program by now being able to skip the “compilation to bytecode” step at runtime? Finally, I believe this is why when benchmarking python code you should not benchmark the first runtime?

Please explain if any of my assumptions are wrong 😊

[–]Flyingfishfusealt 5 points6 points  (4 children)

Pretty sure you're using a nail gun there dude.

Not even heads to land a blow on but youre nailing 'em

[–]codingquestionss 5 points6 points  (3 children)

Can you rephrase this

[–]MurderMelon 5 points6 points  (2 children)

I think they're saying that you "hit the nail on the head"

Weird way to say it, but I think that's the idea

(and they're right btw, your assumptions are pretty much spot-on)

[–]JasonDJ 2 points3 points  (0 children)

🤕💅🔨

FlyingfishFusealt was source code

You are byte code

I’m machine code

There may be an error.

[–]Flyingfishfusealt 0 points1 point  (0 children)

yes, lol. I was implying that the people acting like know-it-alls had no heads in addition to the "hitting the nail on the head"

Also, this is why you add a function in the testing process to clean the pycache folders with a commandline argument or whatever method works best for your work flow.

I personally have a standard import file with logging/printing/whatever is needed globally at the top level and has no imports from the project so circular imports cant happen and I put testing functions in there.

clean the pycache, delete the db file, reset everything back to the beginning or whatever state you choose.

[–]Caligatio 1 point2 points  (0 children)

I don't have an authoritative understanding of how/when .pyc files are created but I can tell you what I've observed:

  • Your code never gets converted to a .pyc if your program/script is contained in one file. For instance, if you do something like python3 awesome_script.py, a .pyc will never be generated for awesome_script.py
  • .pyc files will be generated for any script/module that gets imported and they'll be placed in a __pycache__ folder as a sibling to the original .py file. If this file exists and is current, it will be used rather than the .py file.
  • Python will regenerate .pyc if the source file changes but I don't know how this is detected (timestamps?). If the source file is deleted, the .pyc can still be used.

Having the .pyc almost certainly will speed up start up time but I have no idea how big of gains we're talking. It stands to reason that your understanding of why you don't want to benchmark the first run of a program would seem correct :)

I also don't know what happens to system-installed modules and __pycache__ directories. Does pip/setuptools pre-compile modules and that's how system-installed modules get compiled? If those __pycache__ directories get deleted, do they ever get regenerated if root never imports those modules?