How to convert .py script and .ui file with images to .exe - python

I wrote a program in Python that includes external files, including images and a GUI .ui file.
I want to convert the Python script to a single executable (.exe) file using PyInstaller. When I try to open the .exe file, the program does not open.

You will need more information on why the application is closing immediately in order to solve your problem. To view the error messages associated with running your executable, run the .exe file from the command prompt: /path/to/app/dist/MyApp.exe. This will allow you to observe any errors that may exist after the app was bundled. If the program fails during an import statement, you may need to add a package to the hiddenimports list in the .spec file. Check the PyInstaller documentation for instructions on modifying the .spec file.

Related

Main Python File Using Another Python File (I Want To Convert To .exe)

I Created a Program That Has 2 .py files.
I Want To Make The Program a .exe file, I do it using cx_Freeze.
My Problem Is That I Convert The main.py To .exe but The Second Python File Is Still a .py File.
I Don't Want It Be a .py Because If It Is The User Can See The Code.
If I Also Convert The Second Python File The Program Doesn't Work Because I import The Python File In The Main File.
Any Suggestions?
(I Don't Want To Copy The Second Python File To The Main Python File)
You are chasing the wrong rabbit here. The various tools that generate executable files from Python code are not compilers. They are just tools that embed a Python interpretor with py (or pyc) files to allow users to use the program without a prior Python installation.
Said differently you should not use them to hide your code (except from people not knowing a lot of Python): a pyc does not contain text code but according to the answers to Is it possible to decompile a compiled .pyc file into a .py file? , tools exists that convert back a pyc file into a py file (of course except the comments).
IMHO, you should better google for python obfuscate to find tools dedicated to obfuscation, what neither cx-freeze nor pyinstaller are.
BTW while there are appropriate use cases for obfuscation you should be aware that a determinate attacker can always circumvent it. The reason why the real protection for intellectual property is law and not technics...
I'm not sure how two or more .py files can be converted to .exe.
But in python the easiest way to convert to .exe is a module named pyinstaller .
You can install it using command pip install pyinstaller can get it . After just go to the directory where your project files are and open command prompt in that directory and execute pyinstaller file_name

PyCharm/Pyinstaller - How to optimize distribution

I developed my code using PyCharm and am using PyInstaller to create a desktop .exe application. I am able to create the application, however, my current method requires navigating multiple directories, and to copy/paste some file dependencies. The whole reason to use PyInstaller was to make it more user-friendly, and easy to access the file dependencies. My question is, how should my code be organized so that a general user can easily access the file dependencies, and developer not need to copy/paste the dependency?
Below is my generalized current approach and the result.
To develop using PyCharm, edit:
/projectFolder/main.py,
/projectFolder/helper.py,
/projectFolder/data.xlsx
To create application using PyInstaller:
in command prompt, /projectFolder/venv/Scripts, execute pyinstaller ../../main.py
This creates projectFolder/venv/Scripts/dist/main/main.exe, among many more files (generated by PyInstaller) that the user shouldn't interact with or even see.
At this point, I need to copy/paste /projectFolder/data.xlsx into /projectFolder/venv/Scripts/dist/main for the .exe application to function.
The executable is now ready to be used.
I am looking for a better approach, where the user will see only the relevant files, main.exe and data.xlsx (since this will be modified, periodically). Also, I'd like data.xlsx need not be copy/pasted.
Again, how should I organize my code / package the distribution in order to simplify things for the user?
You can add files or even a folder to install with PyInstaller by using the command --add-data. The documentation explains the format for the file / folder to add but in your case it should be:
pyinstaller --add-data "data.xlsx;." main.py
Now your 'data.xlsx' will automatically copied and pasted into the folder with your .exe file.
PyInstaller also has a feature to compress all your files into one single .exe (including added data) and then when you run your program, it creates a temporary folder in your OS temp folder, usually C:\Users\USERNAME\AppData\Local\Temp\ that starts with _MEIxxxxx where the xxxx is a randomly generated number so your exe programs wont interfer with each other if more than one is running at a time. The folder is automatically deleted upon successful exit of the program.
The code to have only one file instead of a folder:
pyinstaller --onefile --add-data "data.xlsx;." main.py
There are 2 issues with having one file instead of a folder. The first isn't too big of a problem, but everytime your program crashes or doesn't close properly, the temp folder _MEIxxxxx where all your program was unloaded to wont get deleted, potentially clogging up their system. A simple fix would be to add some extra code into your program that checks if there is already a _MEIxxxxx folder with an older date/time in their temp folder and delete it.
The second is that if your program requires to read / write to a file, the code will check in the current directory of the exe, not the temp folder that is created. A workaround to this would be to write some extra code that looks for the a folder that starts with _MEI in their temp and uses that as the path. If it finds more than one folder with _MEI it should take the most recent (and hopefully delete the older ones)
Another cool feature is adding the --runtime-tmpdir command to your pyinstaller which allows you to specify the where you want your _MEIxxxxx to unload, potentially making it easier to check where the data and files you need to run the program.
pyinstaller --onefile --runtime-tmpdir "C:\TemporaryFolder" --add-data "data.xlsx;." main.py
Unfortunately, if you specify a folder or a path that doesn't exit your program wont work at all, so I would recommend creating a separate setup.exe that creates that folder for them and that should be run before running the main.exe file. Afterwards they can delete the setup file and their program should be running.

How do you convert a python script that reads a text file into a working exe

I created an application that reads a text file to get a digit (1 or 0) to execute some code (it runs like a save file since I want the value to be changed permanently, not just while the script is running), but when I use pyinstaller to convert it to a standalone exe file I get a Fatal Error saying that it cannot run the script. Previous compilations from before I added this feature worked, so I know it's something to do with reading that text file.
Note: I tried using a .py file instead of a .txt. While it worked as a script, the same error came up after compilation.
this is what I type into an elevated command prompt:
pyinstaller --onefile --add-data save.txt;.' file.py
You can not edit the data files that you have bundled with your application using --add-data in pyinstaller's --onefile method as it unpacks all the files into a temporary directory when executed, therefore the file wiil not be found in the current directory as the application demands, so you end up with the Fatal Error.
To get around this, refer to this post, use that function in your code and send your relative path into it, so as to get the absolute path of the file when the application is run. Remember, the changes that you make will be lost after you close the application, as these files are treated as "data files", they are not intended to be modified, so the binary will create a new copy of initial file on every application launch.
If you want it to permanently be stored somewhere, you can implement the file creation (if it doesn't exist) form the script itself, by creating a directory in the AppData folder and save the file there.

How to pack a python to exe while keeping .py source code editable?

I am creating a python script that should modify itself and be portable.
I can achieve each one of those goals separately, but not together.
I use cx_freeze or pyinstaller to pack my .py to exe, so it's portable; but then I have a lot of .pyc compiled files and I can't edit my .py file from the software itself.
Is there a way to keep a script portable and lightweight (so a 70mb portable python environment is not an option) but still editable?
The idea is to have a sort of exe "interpreter" like python.exe but with all the libraries linked, as pyinstaller allows, that runs the .py file, so the .py script can edit itself or be edited by other scripts and still be executed with the interpreter.
First define your main script (cannot be changed) main_script.py. In a subfolder (e.g. named data) create patch_script.py
main_script.py:
import sys
sys.path.append('./data')
import patch_script
inside the subfolder:
data\patch_script.py:
print('This is the original file')
In the root folder create a spec file e.g. by running pyinstaller main_script.py.
Inside the spec file, add the patch script as a data resource:
...
datas=[('./data/patch_script.py', 'data' ) ],
...
Run pyinstaller main_sript.spec. Execute the exe file, it should print
This is the original file
Edit the patch script to e.g. say:
print('This is the patched file')
Rerun the exe file, it should print
This is the patched file
Note: As this is a PoC, this works but is prone to security issues, as the python file inside the data directory can be used for injection of arbitrary code (which you don't have any control of). You might want to consider using proper packages and update scripts as used by PIP etc.

Failed to execute python script built using PyInstaller (ModuleNotFoundError)

I am trying to turn a .py file into .exe so that it's runnable on other computers without python. I followed this tutorial, installed pyinstaller then ran the command pyinstaller --onefile IRV.py without the -w since my program runs in the console. It successfully built the .exe file but when I run it it immediately closes even though the program itself asks for input at the beginning. It uses a bunch of .txt and .xslx files from the folder of the .py file so I dragged the .exe out of the dist folder but it still gives the same error. I managed to make a quick print screen before it closes and it gives me this error: https://imgur.com/a/w7TVjaN
The script doesn't even work if I double click the .py file. However it runs perfectly fine if I open up the .py file in an IDE like Spyder. When I run the .exe file it opens up the cmd for a few seconds with nothing written on it and then quickly writes that error I managed to screenshot and then closes. If I double click the .py though it instantly closes without the wait or the error message.
Pyinstaller didn't find the libraries or modules you imported. In the build folder that was deleted in the tutorial, there's a text document that shows warnings for libraries that aren't found by the pyinstaller. You may need to check that.
This Q&A mentioned a problem that is related to your Question: No module error when running python script from command prompt

Categories

Resources