I am trying to create an .exe file such that when run on any windows 10 pc will run without requiring any installations. What I tried using was:
pyinstaller --onefile main.py
I ran the exe file on a friend's computer and it turns out that it required first python2.7 to be installed, then it said that modules weren't installed i.e; no module named datetime found. The executable finally ran after I installed all dependencies on my friend's computer but the whole point of creating .exe file was useless, I might as well have just typed python main.py after doing so much. Is there an appropriate way I could get an .exe file from .py which wont require anything to be installed? Like an actual standalone app. Thank you!
Actually this should work. I never had problems with pyinstaller, yet. Maybe reinstall pyinstaller or make sure to use the newest version. Also try first of all an easy "helloWorld"
input("hello from Console.\nPress Enter to close it :)")
Just tried this one on a colleague's machine that has no python installed. And it worked well.
I've had good results using the pyinstaller module to one file even when running code with multiple dependencies on machines without python installed. Reading the documentation helped There is a comprehensive list of python to exe compilers in another stackoverflow question: a good python to exe compiler?
Related
I’ve recently tried making an .exe from my .py file.
(This is my first time making a .exe)
It’s a long script and includes this cool function.
If I compile this to a .exe
It works just fine on the PC I code on, but not on another PC. Which makes me believe there is something missing during the pip installer ?
To compile this script I need to have ctypes Installed,
and in the cmd say: pyinstaller filename.py
Am I missing something ?
I don't have any error to show unfortunately, it just stays blank after it tries to run wait_until_idle.
The most trusted/automated way that I found to make an exe out of python code is this package:
https://pypi.org/project/auto-py-to-exe/
I tested it myself on multiple situations with whole projects not just single/small scripts.
The main disadvantage of this is the size of the exe file, it's really big and specially if you are using external libraries like pandas, numpy could easily make your exe pass 100 Mega bytes.
It's a simple graphical UI that you could use to generate exe, There is no external dependencies you need to install separately.
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 only one line of code input() written in python and packed with pyinstaller with option --onefile. The exe file is 4577 kB which is almost 5Mb. How can I reduce its size or exclude some auto-bundled libraries?
Ah, You are not creating the build in a separate virtual environment.
Create a virtual environment just for build purpose and install the packages you need in this environment.
in your cmd execute these to create a virtual enviornment
python -m venv build_env
cd build_env
C:\build_env\Scripts\Activate
you will see this >>(build_env) C:\build_env
Install all the packages you need for your script, start with pyinstaller
pip install pyinstaller
Once you are all installed, build the exe as before.
The exe built using the virtual environment will be faster and smaller in size!!
For more details check https://python-forum.io/Thread-pyinstaller-exe-size
The .exe file you create using pyinstaller includes the python interpreter and all modules included in your script.Maybe, the modules you are using have a big library themselves. You can however try using py2exe but it might not work for all projects.The other way to get it smaller is to use a compression program as like, compress the executable using UPX (have a look at this:http://htmlpreview.github.io/?https://github.com/pyinstaller/pyinstaller/blob/v2.0/doc/Manual.html#a-note-on-using-upx).
You can also try excluding some items too but at the discretion that removing such items doesn't interfere with the functionality of your .exe.
I had a similar problem and found a solution. I used Windows terminal preview. This program allows creation of various virtual environments like Windows Power Shell (btw. Linux Ubuntu too. Also, worth noting: you can have many terminals in this program installed and, even, open a few at once. Very cool stuff).
Inside Windows Power Shell in Windows terminal preview I installed all the necessary libraries (like pandas etc.), then I opened the path to my file and tried to use this command:
pyinstaller --onefile -w 'filename.py'
...but, the output exe didn't work. For some reason, the console said that there is a lack of one library (which I had installed earlier). I've found the solution in mimic the auto-py-to-exe library. The command used by this GUI is:
pyinstaller --noconfirm --onedir --console "C:/Users/something/filename.py"
And this one works well. I reduced the size of my output exe program from 911MB to 82,9MB!!!
BTW: 911MB was the size of output made by auto-py-to-exe.
I wonder how is it possible that no one yet has created a compressor that reads the code, checks what libraries are part of the code, then putting only them inside the compression. In my case, auto-py-to-exe probably loaded all libraries that I ever installed. That would explain the size of this compressed folder.
Some suggest using https://virtualenv.pypa.io/en/stable/ but in my opinion, this library is very difficult, at least for me.
NOTE: Before marking this as duplicate please read the whole post first
I'm trying for hours to convert my python 3.6 code to exe, I've searched everywhere and tried everything, the most helpful place was there:
Best method of converting .py to .exe for Python 3.6?
So apparently there are only 3 ways of converting py to exe:
PyInstaller
Py2exe
Cxfreeze
Pyinstaller doesn't support Python 3.6, and py2exe is very outdated.
As for cxfreeze, it sounds like it supports Python 3.6, so that's the route that I've taken.
I've searched everywhere on how to use cxfreeze, unfortunately, all guides that I have found seem to be outdated:
http://cx-freeze.readthedocs.io/en/latest/overview.html this guide for example has three routes, two of which doesn't work (The first gives me a .spec file that i dont know what to do with it and the second gives me an error because i dont have the cxfreeze.util module) and the 3rd is too complicated for my tastes
How do I use cx_freeze? this uses the second route, still it doesn't work
I'm not allowed to comment for some reason, so this isn't really an answer, but for my part I installed cx_Freeze from https://pypi.python.org/pypi/cx_Freeze
When you install it, there's an extra step that's not documented. Using the Windows CMD shell, go to your python installation directory, into the Scripts sub-directory and you will find a few files called cxfreeze...
You need to create the batch file by typing python cxfreeze-postinstall
A file called cxfreeze.bat will be created which you can move into your main python folder or wherever your python program is located.
Then type cxfreeze mypythonprogram.py --target-dir dist
This should create a working exe in the dist directory. However, it doesn't work for me as I get an error in the finder.py file which is part of the cxfreeze installation so I don't know if I've done something wrong or this version is completely buggy.
edit: by commenting out line 561 in the finder.py file (C:\Python\Python36-32\Lib\site-packages\cx_Freeze\finder.py on my system) I was able to run it and Generate the EXE which appears to run OK.
I have created a GUI program using python and wxpython. It is now ready to be turned into an executable file. I have tried for the last two days following various instructions from py2exe. All instructions for Command Prompt refer to older versions of windows and I'm using windows 7. I have never used Command Prompt before as I'm still new to programming. Is py2exe going to best way to create an executable file or is there a better option?
I have followed instructions on creating a setup.py file and it reads:
from distutils.core import setup
import py2exe
setup(windows = ["Core.py"])
When I enter the command in Command Prompt:
C:\Python27\Cylinderdrawer\python setup.py py2exe
I get the following:
'C:\Python27\Cylinderdrawer\python' is not recognized as an internal or eternal command, operable program or batch file
There are more than two options. You can use py2exe, PyInstaller, cx_freeze and bb_freeze. I enjoy using the GUI2Exe wrapper script that wraps all of these plus py2app (for Macs) as it makes tweaking the settings a breeze.
As far as I can tell, cx_freeze, PyInstaller and bb_freeze have the newest releases with py2exe's last release in 2008.
As for your issue, it sounds like you don't have Python on your system path. Try typing out the full path to your python instead. Something like this:
c:\python27\python.exe setup.py py2exe
I don't know what "Cylinderdrawer" is, but that is NOT a standard location for the Python executable on Windows.
See also:
a py2exe tutorial
a GUI2Exe tutorial
a cx_freeze tutorial
a bb_freeze tutorial
a PyInstaller tutorial
Your two options are py2exe and cx_freeze. py2exe is more widely used, and it can be tricky to get all the details right. If you provide details about what is going wrong, we might be able to help.
Thanks everyone for their help. As much as I have not worked it out completely yet, this problem has been solved. I'll ask seperatly about the subsequent problem if I cannot work it out. For anyone else having a similar problem the following is how I solved it.
C:\Python27\Cylinderdrawer\python setup.py py2exe
This was what I originally put into Command Prompt. Cylinderdrawer is my project folder. This is wrong it needs to be where python.exe is located. the next part, i.e., "setup.py" is the setup file. The only way I could get it to work was to explicitly state where the file was. Bellow is the command that worked.
C:\Python27\python "C:\Python27\Cylinderdrawer\setup.py" py2exe
Once again, thanks to all those who helped.