python compile script to "unreadable" form - python

I would like to compile python script, so nobody will be able to easily read source (there is file inside and function to check some hashes - so I want to hide it).
I tried py_compile.compile("script.py") but when I open compiled file (.pyc) in plaintext editor, I can see some strings and function names in readable form. I need to prevent this so it should not be easily readable.
What to use for this?
(I am on linux, python 2.7)

You need python obfuscator. One more way is to compile your script to binary using Nuitka.

Related

How to change default program/ file associations to open files using python script?

I need to make a way to install my code, and set default programs depending on file type.
I've been trying to make my python program easy for people who know nothing about python or programming to use.
As an alternative to an exe I want to make a script to do everything, including putting a shortcut to the python code on the desktop and a start menu folder.
The thing is I need to make sure that .py files automatically hey opened by python, and not the software, psychopy, which the user will install.
Is there a way to use python to change the default program for a given file type?
I've already tried:
Py2exe: I get errors (see post history)
Pyinstaller: I get different errors.
The errors with py2exe seem almost irresolvable.
I've used pyinstaller before and it worked. It won't easily work with this code because it doesn't work with an updated library/package I need. I have library b which depends on library a being up to date. So I can't revert back to a setup that I know how to make work.

What happens when you turn a python file into an executable?

What happens when you turn a python file into an executable? Does it get encrypted? What happens to the imported files? Can you revert it back into a normal .py file?
So I have this python file, let's call this main.py. I also have another file, let's call it scrambler.py. The scrambler.py is an encryptor/decryptor file. So, I imported it to main.py. Then I will turn it into an executable file. Now, we don't want people to see the enryptor/decryptor file. So, can people who doesn't have the source code get the source code of the imported file? Because, from searching, I saw that some people can get the source code of the main code using pyinstxtractor.py. I haven't tried it yet, but can you also get the source code of the imported file? (also do comments get included? I mean they are useless to the program). So that's why, the ultimate question: What happens when you turn a python file into an executable?
The file that I use to turn a python file into an .exe is Pyinstaller and is it different for every converter?
I hope this is a valid question. Thanks in advance.
Pyinstaller essentially bundles a python interpreter along with your python code in a folder. This folder can be put into an installer (using something like inno setup) to be distributed so end users can use it like a normal exe program. It doesn't compile to another language or anything. So no, your code is not private and while you can make it difficult to find certain bits, it is not impossible.
As described here, it is possible to convert to C and to machine code, but pyinstaller won't do that by default for you. Also note that the python bytecode files, while not legible, are not completely uncrackable.
See: https://pyinstaller.readthedocs.io/en/stable/operating-mode.html
See here for more about the encryption option: PyInstaller Encryption --key

Using python to run different files (stata, .stc, etc), as you would a .bat on windows

In this guide to not being a total mess doing research, the authors talk about using a .py file to execute a directory in order -- that is, delete all the output files (.pdf, .txt, etc) and run just the .py and everything will be recreated from the raw data, stata files, maybe other .py's, etc etc.
What is the best way to do this in Python? I know one option is to use subprocesses, but is that the only option? Basically, how can I best mimic a .bat file using Python on a Mac.
You can certainly use Python for shell-script type stuff - with the bonus that it will be relatively portable.
Another option you could consider is "BASH" "(The Bourne Again SHell). That will do everything you can do with .BAT files (and much more). Search for BASH shell scripting.
Whether Python or BASH is the right tool for the job depends on whether you're mostly just writing glue (to call a bunch of other programs) or if you're actually writing complex logic yourself. If it's the former, then I'd go with BASH.

Create an executable file out of .py script

I have a python script I need to be able to run on a computer not having Python installed on it.
I do have found a compile.py example on this link: Can I somehow "compile" a python script to work on PC without Python installed?
When running it as "python compile.py python_script.py" in command prompt, it seems to work, but py2exe seems to compile everything in Python; language-packages etc. This in turn makes the output directories very, very large, when all I really want is an .exe-file. It also takes a very long time to compile.
The script I am using just imports pandas, datetime and a few other packages.
Is the compiler supposed to take that much time and space? Is there another, easier way than explained in the link above?
Thanks
I would recommend using pyinstaller. There's an option for an .exe only. In terms of size, it'll be just as large, but will only be the single exe.

Building a string of Python Code in PyInstaller?

I understand how we can package an .exe by pointing PyInstaller to a file. Such like:
c:\Python25\python c:\Users\Mike\Desktop\pyinstaller-1.4\Makespec.py -F -w sampleApp.py
However is there a way to create an .exe with only a string [and not a file]? Such as:
string="""
print "Hello world"
"""
buildApplication(string) #Function Does not exist
To the best of my knowledge, I am afraid you won't be able to work around the need to create a temporary file with any of the existing standalone executable creation tools (py2exe, PyInstaller and cxFreeze).
What I see as the most viable solution is a bit of security through obscurity, combining the following two techniques:
Create a byte-code compiled file (.pyc) directly, instead of the plaintext .py file, from the generated code string, using the __builtin__.compile function (you can find on the source code of the py_compile module how to achieve this. A byte-compiled file will be significantly less useful to prying eyes than the source file.
A temporary file with an obscure name and location created using the tempfile module. This file will be relatively short lived, but obviously, a sufficiently determined user will be able to find it and copy it while it existes to be consumed by the executable creation tool.

Categories