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.
Related
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
If I obfuscate test.py with Pyarmor, I would run a command like this:
pyarmor obfuscate test.py
However, using this code will generate multiple files.
As shown in the image below, in addition to the file test.py, a folder called pytransform is created.
When I distribute the obfuscated code to users, I must include this subfolder in the distribution. But I don't find this comfortable. I want to distribute the code to users in one file.
If it is a file binarized using PyInstaller, you can also distribute it as one file using innosetup etc. But for non-binary Script, I couldn't find a way to make it a single file.
Is there such a way?
The pytransform folder is generated the exact same every time. It never changes. As a result, if the recipient has a copy of the folder on their system (which they can easily generate using pyarmor), you don't need to send it to them, leaving you with only the one obfuscated file to send to them.
I am writing a short script.py which will update archive.tar.
The tar archive's content is one single file: file.txt (Content: "Hello").
I am using 7za.exe which is the the command line version of 7zip to update file.txt with another file.txt (Content: "Hello world"). I am using the following command:
os.system("7za u archive.tar file.txt")
So far everything is working, however I would like to create an executable from the Python Script using PyInstaller, since not everyone using this script will have python installed.
My question: is there a way to merge both script.py and 7za.exe into one single .exe file?
So far, I have not been able to find an answer.
Why not simply using the ZipFile library?
This could be as simple as:
with ZipFile('spam.zip', 'w') as myzip:
myzip.write('eggs.txt')
If you really want to use a third party then no, there is no simple way of doing what you want to do. The only "clean" way would be to pack your executables together by using an installer, like the ones you could create with Innosetup
NB: You should prefer the use of subprocess rather that os.system, see there https://docs.python.org/fr/3/library/subprocess.html#replacing-os-system
Update: For a tarfile, maybe these could help you:
How to append a file to a tar file use python tarfile module?
How can files be added to a tarfile with Python, without adding the directory hierarchy?
Maybe you'll have to decompress / add / recompress, but you should be able to do it in memory.
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.
My Problem is the following:
I want to create an script that can create other executables. These new executables have to be standalone, so they don't require any DLL's, etc.
I know this is possible with PyInstaller, but only from console/command line.
So essentially, what I want to do is make a python script that imports pyinstaller, creates another .py-file and uses pyinstaller to compile the new script to a .exe, so people who don't have python installed can use this program.
EDIT: The script itself should only use one file, so it can also be a one-file executable
Supposing you have already installed Pyinstaller in PYINSTALLER_PATH (you should have called the Configure.py script in the distribution for the first time), Pyinstaller generates a spec file from your main script by calling the Makespec.py. You can add some flags to generate one dir binary distribution or one file. Finally you have to call Build.py with spec file.
This is easy scriptable with a couple of system calls. Something like :
import os
PROJECT_NAME = "test"
PROJECT_MAIN_SCRIPT = "main_script.py"
MAKESPEC_CMD = """%s %s\Makespec.py -X -n %s -F %s""" % (PYTHON_EXECUTABLE, PYINSTALLER_PATH, PROJECT_NAME, PROJECT_MAIN_SCRIPT)
BUILD_CMD = """%s %s\Build.py %s.spec""" % (PYTHON_EXECUTABLE, PYINSTALLER_PATH, PROJECT_NAME)
os.system(MAKESPEC_CMD)
os.system(BUILD_CMD)
You can avoid to generate the spec file every time and hack it, adding embedded resources (i.e. xml files or configuration) and specifying some other flag. Basically this is a python file, with the definition of some dictionaries.
I don't think there is a Pyinstaller module you can use directly, but you can look at Build.py and mimic its behaviour to do the same. Build.py is the main script which does the trick.
You may want to check out cx_Freeze, which can be used to do this kind of thing.
There are three different ways to use cx_Freeze. The first is to use the included cxfreeze script which works well for simple scripts. The second is to create a distutils setup script which can be used for more complicated configuration or to retain the configuration for future use. The third method involves working directly with the classes and modules used internally by cx_Freeze and should be reserved for complicated scripts or extending or embedding.
Source
Try downloading Pyinstaller's latest development code. There they trying to implement GUI toolkit for building executables.