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.
This question already has answers here:
Create a single executable from a Python project [closed]
(3 answers)
Closed 1 year ago.
I'm building a Python application and don't want to force my clients to install Python and modules.
So, is there a way to compile a Python script to be a standalone executable?
You can use PyInstaller to package Python programs as standalone executables. It works on Windows, Linux, and Mac.
PyInstaller Quickstart
Install PyInstaller from PyPI:
pip install pyinstaller
Go to your program’s directory and run:
pyinstaller yourprogram.py
This will generate the bundle in a subdirectory called dist.
pyinstaller -F yourprogram.py
Adding -F (or --onefile) parameter will pack everything into single "exe".
pyinstaller -F --paths=<your_path>\Lib\site-packages yourprogram.py
running into "ImportError" you might consider side-packages.
pip install pynput==1.6.8
still runing in Import-Erorr - try to downgrade pyinstaller - see Getting error when using pynput with pyinstaller
For a more detailed walkthrough, see the manual.
You can use py2exe as already answered and use Cython to convert your key .py files in .pyc, C compiled files, like .dll in Windows and .so on Linux.
It is much harder to revert than common .pyo and .pyc files (and also gain in performance!).
You might wish to investigate Nuitka. It takes Python source code and converts it in to C++ API calls. Then it compiles into an executable binary (ELF on Linux). It has been around for a few years now and supports a wide range of Python versions.
You will probably also get a performance improvement if you use it. It is recommended.
Yes, it is possible to compile Python scripts into standalone executables.
PyInstaller can be used to convert Python programs into stand-alone executables, under Windows, Linux, Mac OS X, FreeBSD, Solaris, and AIX. It is one of the recommended converters.
py2exe converts Python scripts into only executable on the Windows platform.
Cython is a static compiler for both the Python programming language and the extended Cython programming language.
I would like to compile some useful information about creating standalone files on Windows using Python 2.7.
I have used py2exe and it works, but I had some problems.
It has shown some problems for creating single files in Windows 64 bits: Using bundle_files = 1 with py2exe is not working;
It is necessary to create a setup.py file for it to work. http://www.py2exe.org/index.cgi/Tutorial#Step2;
I have had problems with dependencies that you have to solve by importing packages in the setup file;
I was not able to make it work together with PyQt.
This last reason made me try PyInstaller http://www.pyinstaller.org/.
In my opinion, it is much better because:
It is easier to use.
I suggest creating a .bat file with the following lines for example (pyinstaller.exe must be in in the Windows path):
pyinstaller.exe --onefile MyCode.py
You can create a single file, among other options (https://pyinstaller.readthedocs.io/en/stable/usage.html#options).
I had only one problem using PyInstaller and multiprocessing package that was solved by using this recipe: https://github.com/pyinstaller/pyinstaller/wiki/Recipe-Multiprocessing.
So, I think that, at least for python 2.7, a better and simpler option is PyInstaller.
And a third option is cx_Freeze, which is cross-platform.
pyinstaller yourfile.py -F --onefile
This creates a standalone EXE file on Windows.
Important note 1: The EXE file will be generated in a folder named 'dist'.
Important note 2: Do not forget --onefile flag
You can install PyInstaller using pip install PyInstaller
NOTE: In rare cases there are hidden dependencies...so if you run the EXE file and get missing library error (win32timezone in the example below) then use something like this:
pyinstaller --hiddenimport win32timezone -F "Backup Program.py"
I like PyInstaller - especially the "windowed" variant:
pyinstaller --onefile --windowed myscript.py
It will create one single *.exe file in a distination/folder.
You may like py2exe. You'll also find information in there for doing it on Linux.
Use py2exe.... use the below set up files:
from distutils.core import setup
import py2exe
from distutils.filelist import findall
import matplotlib
setup(
console = ['PlotMemInfo.py'],
options = {
'py2exe': {
'packages': ['matplotlib'],
'dll_excludes': ['libgdk-win32-2.0-0.dll',
'libgobject-2.0-0.dll',
'libgdk_pixbuf-2.0-0.dll']
}
},
data_files = matplotlib.get_py2exe_datafiles()
)
I also recommend PyInstaller for better backward compatibility such as Python 2.3 - 2.7.
For py2exe, you have to have Python 2.6.
For Python 3.2 scripts, the only choice is cx_Freeze. Build it from sources; otherwise it won't work.
For Python 2.x I suggest PyInstaller as it can package a Python program in a single executable, unlike cx_Freeze which outputs also libraries.
Since it seems to be missing from the current list of answers, I think it is worth mentioning that the standard library includes a zipapp module that can be used for this purpose. Its basic usage is just compressing a bunch of Python files into a zip file with extension .pyz than can be directly executed as python myapp.pyz, but you can also make a self-contained package from a requirements.txt file:
$ python -m pip install -r requirements.txt --target myapp
$ python -m zipapp -p "interpreter" myapp
Where interpreter can be something like /usr/bin/env python (see Specifying the Interpreter).
Usually, the generated .pyz / .pyzw file should be executable, in Unix because it gets marked as such and in Windows because Python installation usually registers those extensions. However, it is relatively easy to make a Windows executable that should work as long as the user has python3.dll in the path.
If you don't want to require the end user to install Python, you can distribute the application along with the embeddable Python package.
py2exe will make the EXE file you want, but you need to have the same version of MSVCR90.dll on the machine you're going to use your new EXE file.
See Tutorial for more information.
You can find the list of distribution utilities listed at Distribution Utilities.
I use bbfreeze and it has been working very well (yet to have Python 3 support though).
Not exactly a packaging of the Python code, but there is now also Grumpy from Google, which transpiles the code to Go.
It doesn't support the Python C API, so it may not work for all projects.
Using PyInstaller, I found a better method using shortcut to the .exe rather than making --onefile. Anyway, there are probably some data files around and if you're running a site-based app then your program depends on HTML, JavaScript, and CSS files too. There isn't any point in moving all these files somewhere... Instead what if we move the working path up?
Make a shortcut to the EXE file, move it at top and set the target and start-in paths as specified, to have relative paths going to dist\folder:
Target: %windir%\system32\cmd.exe /c start dist\web_wrapper\web_wrapper.exe
Start in: "%windir%\system32\cmd.exe /c start dist\web_wrapper\"
We can rename the shortcut to anything, so renaming to "GTFS-Manager".
Now when I double-click the shortcut, it's as if I python-ran the file! I found this approach better than the --onefile one as:
In onefile's case, there's a problem with a .dll missing for the Windows 7 OS which needs some prior installation, etc. Yawn. With the usual build with multiple files, no such issues.
All the files that my Python script uses (it's deploying a tornado web server and needs a whole freakin' website worth of files to be there!) don't need to be moved anywhere: I simply create the shortcut at top.
I can actually use this exact same folder on Ubuntu (run python3 myfile.py) and Windows (double-click the shortcut).
I don't need to bother with the overly complicated hacking of .spec file to include data files, etc.
Oh, remember to delete off the build folder after building. It will save on size.
Use Cython to convert to C, compile, and link with GCC.
Another could be, make the core functions in C (the ones you want to make hard to reverse), compile them and use Boost.Python to import the compiled code (plus you get a much faster code execution). Then use any tool mentioned to distribute.
I'm told that PyRun is also an option. It currently supports Linux, FreeBSD and Mac OS X.
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.
So far I have used cx_freeze to convert .py file to .exe file, but I get many files. Is there a way to get it all into one executable?
I have seen that PyInstallerGUI is able to that, but it is for Python 2.7. Can it be done with Python 3.4 as well?
PyInstaller works up to Python 3.5. Once you've installed it (type in your terminal pip install pyinstaller), you can do in your terminal:
pyinstaller --onefile script.py
where script.py is the name of script you want to compile into .exe
With the --onefile option it will create only one .exe file.
I haven't tried it but, PyInstaller says here it can do that and it supports Python 2.7 and Python 3.3+.
Quoting from the linked page:
PyInstaller can bundle your script and all its dependencies into a
single executable named myscript (myscript.exe in Windows).
The advantage is that your users get something they understand, a
single executable to launch. A disadvantage is that any related files
such as a README must be distributed separately. Also, the single
executable is a little slower to start up than the one-folder bundle.
Before you attempt to bundle to one file, make sure your app works
correctly when bundled to one folder. It is is much easier to diagnose
problems in one-folder mode.
I found this in the PyInstaller documentation:
pyinstaller --onefile your-python-file.py
To find more: PyInstaller documentation
I use Ubuntu, Python 3.4 and try to compile my py scripts to exe using py2exe.
Unfortunately When I try to use command 'build_exe', it generates error!
Could anybody help me to solve it or advise how to compile py project to exe in another way?
Error code:
NameError: name 'WinDLL' is not define.
py2exe is for windows...
On linux you can try PyInstaller:
http://www.pyinstaller.org/
PyInstaller is now able to build Windows executables when running
under Linux. See documentation for more details.
The py2exe module needs to be run from within a command prompt shell (aka Windows terminal) to make a Windows executable, since just like pyinstaller, "they use the OS support to load the dynamic libraries, thus ensuring full compatibility". That's why you get the ''WinDLL' is not defined' error when using the py2exe module in a bash shell.
py2exe works until python-3.4 and pyinstaller works until python-3.7. The latest version at time of writing is 3.8. Use the python module virtualenv from command prompt to get the adeqaute python version running (without replacing your current python set-up), and make your executable file. Here is my answer with code. --> Make sure to add python to your Windows path on install, so you can use the pip and python commands in command prompt. Otherwise you have to replace these commands in my answer for their fullpath, e.g. C:\Users\jakethefinn\python37\pip.exe and C:\Users\jakethefinn\python37\python.exe respectively.
If you install python from Microsoft Store, the files pip.exe and python.exe are automatically added to path.