#šŸ”’ What is a pyc file for?

49 messages Ā· Page 1 of 1 (latest)

prime rock
#

I know that it’s a cached precompiled binary version of your Python file, but what is it for?

obsidian ferryBOT
#

@prime rock

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

gray plaza
fluid widget
prime rock
#

Oh, thanks.

hardy prairie
#

What’s in it then?

#

And if there’s a syntax error, does the pyc get made?

fair wyvern
fair wyvern
hardy prairie
fair wyvern
# hardy prairie Is the byte code something you can execute in some way instead of having the pyt...

I believe you can just run it as you would any Python file (assuming the .pyc file was made for that version). And theoretically, yes, it could make things a bit faster since you're skipping the compilation step. So that's what Python does if it sees a .pyc file, it runs that instead of having to recompile the Python file (as long as it hasn't changed). Bytecode is machine-independent so no. It will depend on the version of Python.

fluid widget
#

You shouldn't try to distribute pyc files. They're an implementation detail and there's no guarantee that they'll work anywhere else @hardy prairie @prime rock

raven moss
#

you can technically read, manipulate and write back pyc files using marshal and code objects, but there's no official api to modify the instruction bytes

#

you can dump the instructions with dis

hardy prairie
#

Really interesting

raven moss
hardy prairie
#

So. When we say Python is an interpreted language… it’s not really strictly true. It’s complied and then run on a Python ā€œmachineā€ a bit like Java? Is that correct?

raven moss
#

its very similar to java actually, you just dont see the pyc files as a way to distribute apps

hardy prairie
#

When it’s taught in schools in the uk we teach that it’s interpreted. Now I feel bad that I’m lying.

raven moss
#

but its the same source -> bytecode -> interpreting pipeline

hardy prairie
#

Would you consider Java to be interpreted then?

raven moss
#

kinda

#

but java also jit compiles its bytecode to actual machine code

outer egret
#

Interpretation and compilation and not exclusive of each other. "Compilation" just means "translation" essentially.

hardy prairie
#

I always state that you need source code to run an interpreted program and it reads it line by line. But that might not be the case.

raven moss
raven moss
#

at least the line by line part

hardy prairie
#

Yet I need to teach this as the exams state this. Boo. šŸ˜’

#

Good to know the truth though

hardy prairie
hardy prairie
outer egret
outer egret
raven moss
#

there's also .pyz files, also directly runnable, which are zip bundles of multiple py or pyc files

#

... similar to java's jars

#

python = java confirmed??

raven moss
#

the main feature is still running py files from source

#

but, java can also do that...

#

java = interpreted language??

#

you see its complicated

hardy prairie
obsidian ferryBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.