I've noticed this question several times on SO, but none of the answers seems to be applicable to a Python 3.6 version. As most cases when this is asked, I accidentally deleted my .py source file but I still have a .exe built with pyinstaller (passing only the --onefile and --icon arguments).
I managed to reach the following point:
Ran pyinstxtractor.py on the .exe I mentioned above. This gave me a bunch of files including one with no extension which is actually the .pyc of the source file missing the magic number.
Renamed the file with no extension in order to have the .pyc extension
Used a hex editor to append the magic number to the .pyc file, using the header from another .pyc file (used __future__ for this example). It looks like this.
Ran uncompyle6 on the .pyc file that now has the correct magic number, but the operations fails as seen here. Some of the code is retrieved(about 15%) but this important part is just obfuscated byte-code.
Any other tools I managed to come across do not work on Python 3.6 such as Easy Python Decompiler, unpyclib, pyREtic.
I did not manage to use pycdc/Decompyle++ or Maynard as I don't understand how they work, but they still don't seem to support 3.6.
Anything else I might try?
PS: My OS is Win7 and I did try file recovery software and system rollbacks, none of which worked.
Related
I am developing a paid application in Python. I do not want the users to see the source code or decompile it. How can I accomplish this task of hiding the source code from the user, but running the code perfectly with the same performance?
You may distribute the compiled .pyc files which is a byte code that the Python interpreter compiles your .py files to.
More info on this found here on stackoverflow.
How to compile all your project files.
This will somewhat hide your actual code into bytecode, but it can be disassembled. To prevent from disassembling you need to use obfuscation. Pyarmor might be something you're looking for.
You will definitely see the code if you're running it as a Python file. Maybe try using pyinstaller to make a executable binary for the respective Operating System that you're building for.
The best way would be to turn your python code into an executable file.
When u take a look here, there is a nice Tutorial on how to do it:
Install pyinstall via pip3 install pyinstaller
Pack your excecutable with pyinstaller main.py
There is a lot of options to tweak the output of your application, the docs can be found under https://pyinstaller.org/en/stable/
I have a small program I am trying pass around to friends. It includes nonstandard libraries like the PIL and mtTkinter packages, and thus usually requires them to be included in the pythonpath.
Is there a way I can generate the the .pyc file so that all required code for the program to run is included in the file, and not required to be installed on my friends computers. I want this to be able to work between different OS like windows, mac, and linux.
Does it have to a pyc file? I use pyinstaller to package programs and share. The first time I used it, I couldn't believe how easy it is.
Here's a link to the portion on packaging for multiple operating systems: https://pyinstaller.readthedocs.io/en/stable/usage.html#supporting-multiple-operating-systems
I want to know what a pyc file(python bytecode) is. I want to know all the details.
I want to know about how pyc files interface with the compiler. Is it a replacement for exe?
Does it need to be run by python?
Is it as portable as the .py file is?
Where should I use this?
To supplement Mike Graham's answer there are some interesting comments here giving some information on pyc files. Most interestingly I suspect for you is the line:
A program doesn't run any faster when it is read from a ‘.pyc’ or ‘.pyo’ file than when it is read from a ‘.py’ file; the only thing that's faster about ‘.pyc’ or ‘.pyo’ files is the speed with which they are loaded.
Which hits the nail on the head w.r.t. the crux of a pyc file. A pyc is a pre-interpreted py file. The python bytecode is still the same as if it was generated from a py file - the difference is that when using a pyc file you don't have to go through the process of creating that pyc output (which you do when running a py file). Read as you don't have to convert the python script to python bytecode.
If you've come across .class files in java this is a similar concept - the difference is in java you have to do the compiling using javac before the java interpreter will execute the application. Different way of doing things (the internals will be very different as they're different languages) but same broad idea.
Python bytecode requires Python to run, cannot be ran standalone without Python, and is specific to a particular x.y release of Python. It should be portable across platforms for the same version. There is not a common reason for you to use it; Python uses it to optimize out parsing of your .py file on repeated imports. Your life will be fine ignoring the existence of pyc files.
From the docs:
As an important speed-up of the start-up time for short programs that use a lot of standard modules, if a file called spam.pyc exists in the directory where spam.py is found, this is assumed to contain an already-“byte-compiled” version of the module spam. The modification time of the version of spam.py used to create spam.pyc is recorded in spam.pyc, and the .pyc file is ignored if these don’t match.
See the ref for more info. But some specific answers:
The contents of the spam.pyc file are platform independent, so a Python module directory can be shared by machines of different architectures.
It's not an executable; it's used internally by the compiler as an intermediate step.
In general, you don't make .pyc files by hand: the interpreter makes them automatically.
We've got a (Windows) application, with which we distribute an entire Python installation (including several 3rd-party modules that we use), so we have consistency and so we don't need to install everything separately. This works pretty well, but the application is pretty huge.
Obviously, we don't use everything available in the runtime. I'd like to trim down the runtime to only include what we really need.
I plan on trying out py2exe, but I'd like to try and find another solution that will just help me remove the unneeded parts of the Python runtime.
One trick I've learned while trimming down .py files to ship: Delete all the .pyc files in the standard library, then run your application throughly (that is, enough to be sure all the Python modules it needs will be loaded). If you examine the standard library directories, there will be .pyc files for all the modules that were actually used. .py files without .pyc are ones that you don't need.
Both py2exe and pyinstaller (NOTE: for the latter use the SVN version, the released one is VERY long in the tooth;-) do their "trimming" via modulefinder, the standard library module for finding all modules used by a given Python script; you can of course use the latter yourself to identify all needed modules, if you don't trust pyinstaller or py2exe to do it properly and automatically on your behalf.
This py2exe page on compression suggests using UPX to compress any DLLs or .pyd files (which are actually just DLLs, still). Obviously this doesn't help in trimming out unneeded modules, but it can/will trim down the size of your distribution, if that's a large concern.
I'm trying to create a self-contained version of pisa (html to pdf converter, latest version), but I can't succeed due to several errors. I've tried py2exe, bb-freeze and cxfreeze.
This has to be in windows, which makes my life a bit harder. I remember that a couple of months ago the author had a zip file containing the install, but now it's gone, leaving me only with the python dependent way.
How would you work this out?
Check out pyinstaller, it makes standalone executables (as in one .EXE file, and that's it).