#š What is a pyc file for?
49 messages Ā· Page 1 of 1 (latest)
@prime rock
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.
it can improve runtime of subsequent runs
that's all it is. you can just ignore that it's there.
if you try to run a py file, and it hasn't changed since the last time a pyc file was created for it, python will secretly use the pyc file.
Oh, thanks.
They contain Python bytecode which is basically instructions that are generated from your code and that are then run by Python.
And no, syntax errors are detected way before bytecode is generated. But for other runtime errors that don't inmediately happen when the module is imported, I'd imagine that yeah.
Is the byte code something you can execute in some way instead of having the python file? Is that possible somehow in order to make things more efficient? And is the bytecode unique to each processor? X
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.
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
a pyc file contains python bytecode, which is interpreted by your python interpreter. it's cross platform but rarely cross version
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
Really interesting
i made https://github.com/0x3C50/pyasm a while back, it can actually generate python 3.11 code objects you can then write as a pyc with marshal
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?
correct
its very similar to java actually, you just dont see the pyc files as a way to distribute apps
When itās taught in schools in the uk we teach that itās interpreted. Now I feel bad that Iām lying.
but its the same source -> bytecode -> interpreting pipeline
no it is still interpreted in a sense
Would you consider Java to be interpreted then?
Interpretation and compilation and not exclusive of each other. "Compilation" just means "translation" essentially.
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.
python also recently started doing that btw
yeah thats false
at least the line by line part
Yet I need to teach this as the exams state this. Boo. š
Good to know the truth though
Well, I tend to state that compiled code doesnāt need the source code for it to run.
So Python has sort of changed to a compiled bytecode language from an interpreted language recently.
That's not false I don't think, but it's not a great distinction.
I don't think Python was ever strictly interpreted. In fact, I don't know of any non-trivial language that's strictly interpreted. Intermediate bytecode compilation is very common.
fun fact: python can run pyc files directly. in that sense its similar to java
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??
eh
the main feature is still running py files from source
but, java can also do that...
java = interpreted language??
you see its complicated
Yeah. Fascinating.
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.