I am trying to run a python file that I create as an executable or .exe in windows. I want to run this code in another computer that does not have nor need python, or pyserial or PIL or any other software, My code creates a Gui that do stuff
I am using a method to do this but it is not working
first I create a new python file, called setup.py, inside I put this code
from distutils.core import setup
import py2exe
setup(console=['mycode.py'])
and mycode.py is where my code creates a GUI that do stuff
of course I have downloaded the py2exe application by typing in the command prompt
pip install py2exe
then in the command prompt I change the directory where my setup.py and mycode.py are and run this line
python setup.py py2exe
and effectively it do creates 2 folders, one with pycache (with one archive mycode.cpython-34.pyc) and another one with a lot of archives and mycode.exe
then I take these to folders from my computer to another computer where python, pyserial, PIL are not installed, and I try to run mycode.exe and it's not working. Could someone explain why?
I have found the problem, there is no problem with py2exe but with mycode
in mycode I use some images from my computer so when I create the .exe it takes those images from the directory in my computer and runs
but when I take the .exe from my computer to another computer to run it, I do not take those images and therefore the .exe does not run
now the problem is how to call these images, which is another question
Related
I want to create a single executable from my Python project which uses tkinter. A user should be able to download and run it without needing Python installed. What can I use to build a self-contained executable from a Python project?
my question is somewhat similar to this one but I didn't find what I was looking for there.
I have a folder which looks like this:
-project/
-script.py
-res/
-image1.jpg
-image2.jpg
-image3.jpg
In my script.py I am using the images from the res folder in my tkinter UI and I want to create an executable out of this script. what would be the best way to tackle this problem?
You can try using PyInstaller
PyInstaller - How to convert to .exe
1. Installation
The pyinstaller module makes you able to create a standalone exe file for pc from a python script, so that you can use it on other computers than do not have python or your python version.
To install pyinstaller: pip install
2. Create an exe
After you installed pyinstaller, go in the folder where your python script to be transformed in exe is and type in the command line (go in the address bar on top of the window and type cmd). Then type :
pyinstaller yourscript.py
where your script is the name of your file.
You will find a folder called “dist” where there is a folder called yourscript (if the name of your script was this) where there are many files and the exe file that will allow you to run the script also on other computer. You just have to copy the whole folder.
3. What to do if there are images
If you use images, that are in the same folder, for example, you can copy manually into the folder made by pyinstaller or you can run this command to make it do it for you:
pyinstaller –add-data “*.png;.” yourscript.py
This way you will see that in the folder where your exe is there are also the png files (all of them, if you want just one do not put the asterisc, but the whole name of the file.
External links - PyInstaller
Use a bundler like Pyinstaller.
Pyinstaller website
Include images in Pyinstaller
Note: If you are having issues with tkinter and pyinstaller take a look at this: Problems with Pyinstaller with tkinter app on python 3.5
I'm trying to run pyinstaller in python exe file in order to someone without python can use pyinstaller but no idea how to do it.
I tried import PyInstaller with other needed modules and convert this script to exe but I got error "The 'PyInstaller' distribution was not found and is required by the application". I also tried to pack PyInstaller exe file but didn't worked too. Python 3.6.5
Any ideas how to do it?
Unfortunately, what you're describing is not possible with PyInstaller. I submitted an issue on GitHub, and this is what one of the developers said:
Nope, this won't work.
PyInstaller internally uses a lot of sub-processes where it is assumed that sys.executable points to a python interpreter (e.g., here); this is not true in a frozen application, where it points to the frozen executable, which ends up in effectively endless recursion.
Even if this was not a problem, the frozen application includes only a subset of python environment, so it cannot be used to freeze an arbitrary script (not to mention the issue of 3rd party packages).
So whatever use case you have for this, it cannot be supported by PyInstaller.
check the requirements for the Pyinstaller from this link initially
https://pythonhosted.org/PyInstaller/requirements.html
Then install Pyinstaller by,
pip install pyinstaller
To create .exe file use this command for basically,
pyinstaller your_script.py
To run the created .exe
METHOD 1
Do double click the .exe file in your directory.
METHOD 2
In your cmd prompt load in to your current directory or project directory then do
: .\dist\your_script.exe
because the create .exe files are saved in dist folder inside to the project folder with the name of your script file names only.
I have a pygame application which I would like to convert to .exe format.
Pygame2exe (https://pygame.org/wiki/Pygame2exe) works nicely EXCEPT I am unable to figure out how to do this conversion with a project that has more than one custom module.
For example:
python_project/
main.py
other.py
I need to compile both modules, and merging them is not an option.
I have been working at this problem for a few weeks, and have not found any solutions.
I know it is possible, because I had it working, and then formatted the only hard drive that had a copy of the code without realizing I had not made a backup.
EDIT
Thank you Michael.
I just had to make a small change to the setup.py file you provided to make it font compatible. Here is the full setup.py file
from distutils.core import setup
import py2exe
import os
origIsSystemDLL = py2exe.build_exe.isSystemDLL
def isSystemDLL(pathname):
if os.path.basename(pathname).lower() in ["sdl_ttf.dll"]:
return 0
return origIsSystemDLL(pathname)
py2exe.build_exe.isSystemDLL = isSystemDLL
setup(windows=['main.py'])
(taken from Pygame font not working after py2exe)
Thanks again.
if you are using python 3.3 or 3.4, you can use py2exe.
install through pip, pip install py2exe in cmd
note: if 'pip' is not recognized as an internal or external command, navigate to the directory in which python is installed,/scripts probably: C:\python34\scripts
make a setup script called setup.py,
with just the three lines of code
from distutils.core import setup
import py2exe
setup(windows=['filename.pyw'] #probably pyw in windows, will be name of main file
then open command prompt in the directory where the main file and setup.py are located, and type setup.py py2exe
DO NOT USE or filename.pyw in the setup script, use the name of the main module.
this will make a folder called dist, (you can safely get rid of '__pycache_') containing all the files your exe needs to run!
you will probably want to make an installer, I would recommend Inno Setup (http://www.jrsoftware.org/isinfo.php)
I have been working on a python GUI based project using PyQt5. I am done with that and it runs perfectly on my PC but I want to make an executable out of it. So, i'm using cx_freeze to convert my python file to an executable. I have python version 3.5 and PyQt5.
When I build the script the build folder is made and when I run the executable it get the missing modules error as shown in the image attached.
Kindly, help me out i am stuck on this for ages now.
Copy all the modules in the same directory as the file you are converting to exe, then try it again.
How do I convert the following files into one executable?
A main.py file that imports five other python scripts
Five python scripts that each have their own GUI
A background image is used in the main.py file
I am using Python 3.5.2. I have tried py2exe, cx_Freeze and pyinstaller but none seem to work, or I am doing something very wrong. Please could you help with clear steps.
It seems I have to downgrade to Python 3.4 in order to convert successfully but I don't really want to downgrade.
I am using tkinter for GUI and the Python math module for rounding-off numbers.
I've had some success with pyinstaller and a program using enaml (Qt backend) for the GUI.
For pyinstaller it helps to use the --debug option or make a .spec file and set debug=True. Also, pyinstaller has the option to make a single folder with an exe in the folder instead of a single exe, this might be easier to debug. You can distribute your single-folder programme by making a single-exe installer with software like InnoSetup.