Read compiled python file

How to run a pyc compiled python file

Solution 2: To decompile compiled .pyc python3 files, I used in my current Ubuntu OS as follows: Installation of uncompyle6: To create a .py file from .pyc file Run: Automatically a new .py file will be created with the same existing .pyc file name. The .pyc contain the compiled bytecode of Python source files, which is what the Python interpreter compiles the source to.

How do I open a compiled python file (.pyc)

The easiest way to see what goes into .pyc files is to disassemble a python function

import dis def f(): x = 1 a = x print(a, x) dis.dis(f) 

and the output should look something like this

 2 0 LOAD_CONST 1 (1) 2 STORE_FAST 0 (x) 3 4 LOAD_FAST 0 (x) 6 STORE_FAST 1 (a) 4 8 LOAD_GLOBAL 0 (print) 10 LOAD_FAST 1 (a) 12 LOAD_FAST 0 (x) 14 CALL_FUNCTION 2 16 POP_TOP 18 LOAD_CONST 0 (None) 20 RETURN_VALUE 

Every operation you see here ( LOAD_CONST , STORE_FAST etc.) has an associated operation code (opcode), also known as bytecode. Each bytecode is stored in its binary form in the .pyc file, alongside the «lists» of constants, variables, functions, etc.

It’s more to explain to bytecodes, but if you’re really interested how it works, there are plenty good articles online about them. In the meantime you can check this one.

If you have understood them and liked the matter, you can try learning assembly language.

Читайте также:  Python socket send int

If I understood your question correctly, you’re trying to execute a .pyc file as if it were an actual program.

A .pyc file is not an executable, it is Python bytecode meant to be ran by the Python interpreter. It compiles scripts to this before running them.

If you’d like to disassemble the Python bytecode, try the dis module.

>>> import helloworld >>> import dis >>> dis.dis(helloworld) Disassembly of main: 2 0 LOAD_GLOBAL 0 (print) 2 LOAD_CONST 1 ('Hello, world!') 4 CALL_FUNCTION 1 6 POP_TOP 8 LOAD_CONST 0 (None) 10 RETURN_VALUE >>> 

Pyc — Calling python compiled files in Python 3, Calling python compiled files in Python 3. I have multiple Python versions installed (2.7 and 3.4) I want to run a .pyc with specified version of Python. #! C:\python34\python import sys print («Hello»,sys.version.split () [0]) input () This sheebang works fine on Windows because I use pylauncher So I can compile like …

How to run a .pyc (compiled python) file?

Since your python file is byte compiled you need to run it through the python interpreter

The reason you can run your .py files directly is because you have the line

or something similar on the first line in the .py files. This tells your shell to execute the file with the Python interpreter.

To decompile compiled .pyc python3 files, I used uncompyle6 in my current Ubuntu OS as follows:

    Installation of uncompyle6:

uncompyle6 -o . your_filename.pyc 

Python compiles the .py files and saves it as .pyc files so it can reference them in subsequent invocations. The .pyc contain the compiled bytecode of Python source files, which is what the Python interpreter compiles the source to. This code is then executed by Python’s virtual machine. There’s no harm in deleting them (.pyc), but they will save compilation time if you’re doing lots of processing.

Python is an interpreted language, as opposed to a compiled one, though the distinction can be blurry because of the presence of the bytecode compiler. Compiling usually means converting to machine code which is what runs the fastest. But interpreters take human-readable text and execute it. They may do this with an intermediate stage.

For example, When you run myprog.py source file, the python interpreter first looks to see if any myprog.pyc exists (which is the byte-code compiled version of myprog.py ), and if it is as recent or more recent than myprog.py . If so, the interpreter runs it. If it does not exist, or myprog.py is more recent than it (meaning you have changed the source file), the interpreter first compiles myprog.py to myprog.pyc .

There is one exception to the above example. If you put #! /usr/bin/env python on the first line of myprog.py , make it executable, and then run myprog.py by itself.

How to compile python program ( convert to pyc) in, Thanks @meissner_. import py_compile. py_compile.compile («file.py») #compiles single file named file.py. python -m compileall ./ #combines all programs under current directory. Both approaches work in Python2 and Python3. Only difference in Python2 and Python3 is: Python2 generates .pyc file in the …

How can I import a .pyc compiled python file and use it

Unfortunately, no , this cannot be done automatically. You can, of course, do it manually in a gritty ugly way.

Setup:

For demonstration purposes, I’ll first generate a .pyc file. In order to do that, we first need a .py file for it. Our sample test.py file will look like:

def foo(): print("In foo") if __name__ == "__main__": print("Hello World") 

Super simple. Generating the .pyc file can done with the py_compile module found in the standard library. We simply pass in the name of the .py file and the name for our .pyc file in the following way:

 py_compile.compile('test.py', 'mypyc.pyc') 

This will place mypyc.pyc in our current working directory.

Getting the code from .pyc files:

Now, .pyc files contain bytes that are structured in the following way:

  • First 4 bytes signalling a ‘magic number’
  • Next 4 bytes holding a modification timestamp
  • Rest of the contents are a marshalled code object.

What we’re after is that marshalled code object, so we need to import marshal to un-marshall it and execute it. Additionally, we really don’t care/need the 8 first bytes, and un-marshalling the .pyc file with them is disallowed, so we’ll ignore them ( seek past them):

import marshal s = open('mypyc.pyc', 'rb') s.seek(8) # go past first eight bytes code_obj = marshal.load(s) 

So, now we have our fancy code object for test.py which is valid and ready to be executed as we wish. We have two options here :

  1. Execute it in the current global namespace. This will bind all definitions inside our .pyc file in the current namespace and will act as a sort of: from file import * statement.
  2. Create a new module object and execute the code inside the module. This will be like the import file statement.
Emulating from file import * like behaviour:

Performing this is pretty simple, just do:

This will execute the code contained inside code_obj in the current namespace and bind everything there. After the call we can call foo like any other funtion:

Emulating import file like behaviour:

This includes another requirement, the types module. This contains the type for ModuleType which we can use to create a new module object. It takes two arguments, the name for the module (mandatory) and the documentation for it (optional):

m = types.ModuleType("Fancy Name", "Fancy Documentation") print(m)

Now that we have our module object, we can again use exec to execute the code contained in code_obj inside the module namespace (namely, m.__dict__ ):

Now, our module m has everything defined in code_obj , you can verify this by running:

These are the ways you can ‘include’ a .pyc file in your module. At least, the ways I can think of. I don’t really see the practicality in this but hey, I’m not here to judge.

Python — Using PYC file instead of a PY, 1 Answer. Normally, python is not compiled. The .pyc files are only a performance optimization that improve loading times. Python is looking for the .py file because it always checks that first. If a .pyc file is newer than its corresponding .py file, it will then use the .pyc file. If the .py file is newer it will create a new .pyc …

Calling python compiled files in Python 3

#! C:\python34\python import print # imports print.pyc #now you can use the pyc as a module. DoSomething() 

Can C run compiled Python code (.pyc files)?, First, you cannot run anything in C, C is a programming language. You can embed python in your C program (see Documentation ), because python can be compiled as library. But Python is not only the pyc-File, but consists of many modules, that make python as powerful as it is. Share Improve this answer …

Источник

How to decompile compiled .pyc python files to find/see original source code

Hello,everyone.In this post,i am sharing how to decompile compiled python files which are in .pyc formats usually.It may be useful for reverse enginering and if you want to know the original source code of any compiled python files.It may be also useful whenever your original .py python file is deleted unknowingly and there is only compiled .pyc file left on disk and you want your original .py file back.In all those cases, this method can be used.

I have used Ubuntu OS here and it can be used on Windows OS also.

Tools Required :

Thanks : R. Bernstein (Author of uncompyle6)

1.Install uncompyle6 by using pip on Terminal or you can download from above link and install it by running its setup.py file.To install uncompyle6 by using pip ,use this command on Terminal :

2.After installation of uncompyle6 ,you can check its successfull installation and its usage by running uncompyle6 command on Terminal (see screenshot).

3.To decompile any file in current diectory ,use command (see screenshot for example): uncom pyle6 -o .

Example : uncompyl6 -o . txfile.pyc rxfile.pyc

Screenshot from 2017-07-01 13-38-05

4.You will see your decompiled files created with same name in the current working directory(or desired directory) .Now,you can open this decompiled file with any Text Editor and see original source code.

Screenshot from 2017-07-01 13-38-46.png

5.if you want to see decompiled code on terminal(standard output),use command(see screenshot for example ): uncompyle6

Screenshot from 2017-07-01 13-39-59.png

Thanks for reading my post.I hope this will be useful for python users who want to decompile compiled python files.

Источник

Оцените статью