Pyinstaller как создать exe

Pyinstaller как создать exe

PyInstaller — How to Turn Your Python Code into an Exe on Windows

You have just created an awesome new application. Maybe it’s a game or maybe it’s an image viewer. Whatever your application is, you want to share it with your friend or a family member. However, you know they won’t know how to install Python or any of the dependencies. What do you do? You need something that will transform your code into an executable!

Python has many different tools you can use to convert your Python code into a Windows executable. Here are a few different tools you can use:

  • PyInstaller
  • py2exe
  • cx_freeze
  • Nuitka
  • Briefcase

These various tools can all be used to create executables for Windows. They work slightly differently, but the end result is that you will have an executable and perhaps some other files that you need to distribute too.

PyInstaller and Briefcase can be used to create Windows and MacOS executables. Nuitka is a little different in that it turns your Python code into C code before converting it into an executable. What this means is that the result ends up much smaller than PyInstaller’s executable.

However, for this article, you will focus on PyInstaller. It is one of the most popular packages for this purpose and has a lot of support. PyInstaller also has good documentation and there are many tutorials available for it.

In this article, you will learn about:

  • Installing PyInstaller
  • Creating an Executable for a Command-Line Application
  • Creating an Executable for a GUI

Let’s transform some code into a Windows executable!

Installing PyInstaller

To get started, you will need to install PyInstaller. Fortunately, PyInstaller is a Python package that can be easily installed using pip :

This command will install PyInstaller and any dependencies that it needs on your machine. You should now be ready to create an executable with PyInstaller!

Creating an Executable for a Command-Line Application

The next step is to pick some code that you want to turn into an executable. You can use the PySearch utility from chapter 32 of my book, Python 101: 2nd Edition, and turn it into a binary. Here is the code:

Next, open up a Command Prompt (cmd.exe) in Windows and navigate to the folder that has your pysearch.py file in it. To turn the Python code into a binary executable, you need to run the following command:

If Python isn’t on your Windows path, you may need to type out the full path to pyinstaller to get it to run. It will be located in a Scripts folder wherever your Python is installed on your system.

When you run that command, you will see some output that will look similar to the following:

PyInstaller is very verbose and will print out a LOT of output. When it is finished, you will have a dist folder with a pysearch folder inside of it. Within the pysearch folder are many other files, including one called pysearch.exe . You can try navigating to the pysearch folder in your Command Prompt and then run pysearch.exe :

That looks like a pretty successful build! However, if you want to give the executable to your friends, you will have to give them the entire pysearch folder as all those other files in there are also required.

You can fix that issue by passing the —onefile flag, like this:

The output from that command is similar to the first command. This time when you go into the dist folder though, you will find a single file in there called pysearch.exe instead of a folder full of files.

Creating an Executable for a GUI

Creating an executable for a GUI is slightly different than it is for a command-line application. The reason is that the GUI is the main interface and PyInstaller’s default is that the user will be using a Command Prompt or console window. If you run either of the PyInstaller commands that you learned about in the previous section, it will successfully create your executable. However, when you go to use your executable, you will see a Command Prompt appear in addition to your GUI.

You usually don’t want that. To suppress the Command Prompt, you need to use the —noconsole flag.

To test out how this would work, grab the code for the image viewer that was created with wxPython from chapter 42 of Python 101: 2nd Edition. Here is the code again for your convenience:

To turn this into an executable, you would run the following PyInstaller command:

Note that you are not using the —onefile flag here. Windows Defender will flag GUIs that are created with the —onefile as malware and remove it. You can get around that by not using the —onefile flag or by digitally signing the executable. Starting in Windows 10, all GUI applications need to be signed or they are considered malware.

Microsoft has a Sign Tool you can use, but you will need to purchase a digital certificate or create a self-signed certificate with Makecert, a .NET tool or something similar.

Wrapping Up

There are lots of different ways to create an executable with Python. In this article, you used PyInstaller. You learned about the following topics:

  • Installing PyInstaller
  • Creating an Executable for a Command-Line Application
  • Creating an Executable for a GUI

PyInstaller has many other flags that you can use to modify its behavior when generating executables. If you run into issues with PyInstaller, there is a mailing list that you can turn to. Or you can search with Google and on StackOverflow. Most of the common issues that crop up are covered either in the PyInstaller documentation or are easily discoverable through searching online.

PyInstaller Tutorial

PyInstaller can be used to create .exe files for Windows, .app files for Mac, and distributable packages for Linux. Optionally, it can create a single file which is more convenient for distributing, but takes slightly longer to start because it unzip itself.

This tutorial walks through the process of installing and using PyInstaller to create distributable packages.

Installation

To install PyInstaller, use pip and install the pyinstaller package.

Simple build

PyInstaller is not a cross-compiler, so it will only build a package for your current system. So, you must build Windows from Windows, Mac from Mac, and Linux from Linux.

The output will be in a folder named dist/ .

Build with .spec file

For more complex builds, it can help to first create a .spec file which defines all the files that should be included and any other settings. You can read more about spec files at https://pythonhosted.org/PyInstaller/spec-files.html.

Generate the .spec file

To generate a .spec file, use pyi-makespec which comes with the pyinstaller package. Provide pyi-makespec the Python file/application you want to package and it will generate the spec file.

Note that you can pass all the regular command-line flags that you would pass to pyinstaller to pyi-makespec and it will include those options in the spec file.

Tweak the .spec file

Modify the .spec file to meet your needs. This may include adding resource files like images, icons, configuration files, or the Designer .ui templates. Additionally, you might want to disable the console if it is a GUI application.

Include files

To add arbitrary files, modify the line in the .spec file that contains datas=[] . It should contain a list of tuples. Each tuple should contain two values, the file(s) to include, and the target directory. For example, to include all .ui files in your templates/ directory in to a templates/ directory of your final distributable package, modify the spec file like this:

Disable console

If you have a GUI application, such as a PyQt5, Tkinter, PyGame, or GTK+ application, you might want to disable the console so a window with the command prompt does not show up in addition to the regular desired GUI window. You can disable the console by changing Console=True in the .spec file to Console=False like this:

Change the file icon

If you need to create a .ico icon file, you could use one of the following:

Inedo’s Icon Maker can be downloaded from the GitHub releases as a .zip file. Unzip the file to get IconMaker.exe . Run it, and drag your image file in to the window. It accepts up to 256×256 pixel images.

Icoconvert.com allows you to upload an image and get a .ico file back.

To change the icon that the .exe has, you can pass the —icon=icon.ico to the pyi-makespec command or modify the .spec file yourself by altering the exe object to add the argument icon=’icon.ico’ .

Build the package

Once the .spec file is up to your standards, use pyinstaller and pass it the .spec file as the first argument. It will use the information specified in the file to create the final distributable package.

The output will be in the dist/ folder the same as building without the .spec file.

Conclusion

After reading this tutorial, you should be able to create distributable Python applications using PyInstaller.

PyInstaller – Create Executable Python Files

PyInstaller Python

Hey folks! In this tutorial, we will learn the purpose and the basics of Python’s PyInstaller. So, let’s get started!

What is PyInstaller?

PyInstaller is a package in Python which bundles all the dependencies of Python applications in a single package.

We don’t need to install different packages or modules for different applications.

PyInstaller reads and analyzes our code and then discovers the modules that our program requires in order to execute. It then packages them into a single folder or a single executable file.

It is used of create .exe files for windows, .app files for Mac and distributable packages for Linux.

How to install PyInstaller?

We can download PyInstaller from PyPI. We can install it using pip package manager.

It is recommended to create a virtual environment and install PyInstaller there.

Enter the following command in windows command prompt-

Now, set the current directory to the location of your program program.py .

Run the code given below. Here,program.py is the name of our given python script.

PyInstaller analyzes our code and does the following-

  1. Creates a program.spec file which contains the information about the files that should be packed up.
  2. A build folder is created which contains some log files and working files.
  3. A folder named dist will also be created which contains a .exe file with the same name as the given python script name.

Now there are 3 important elements of using the PyInstaller:

  • The .spec file
  • The build folder
  • The dist folder

What is a spec file?

A Spec file, short for specification file is the first file that is built after the execution of pyinstaller program.py . It is stored in —specpath= directory . The spec file is an executable python code that tells PyIstaller how to process our Python script.

Modification of the spec file is not needed except in few cases where it is useful, when we want to:

  1. To bundle our data files with the app.
  2. To include run-time libraries that are unknown to PyInstaller.
  3. To add Python run-time options to the executable.
  4. To create a multiprogram bundle with merged common modules.

To create a spec file, run this command-

This command creates program.py spec file. To build the application, we pass the spec file to pyinstaller command:

What is build folder?

The build folder stores metadata and is useful for debugging.

build/program/warn-program.txt file in the build/ folder contains a lot of outputs to understand things better.

To review the output, rebuild the executable with —log-level=DEBUG option. It is recommended to save this output in order to refer to later. We can do this by-

This will create a build.txt file in the build folder which will contain many DEBUG messages.

What is the dist folder?

The dist folder contains the requirements and executables for the application. It contains a .exe file with the same name as our script.

The executable to run is dist/program/program.exe on windows.

What is ImportError ?

If PyInstaller is unable to detect all the dependencies properly, an ImportError error is seen. This happens when we use __import__( ) . imports inside functions or hidden imports.

To resolve this we use, —hidden-import . This command includes packages and modules automatically.

Another way is using hook files that contain additional information that helps PyInstaller package up a dependency.

How to change name of our executable using PyInstaller?

To avoid our spec, build and executable file being named same as our python script name, we can use –name command.

How to create a single executable file?

PyInstaller can also create a one-file app for our Python script. It contains an archive of all Python modules required by our python program.

To create only one executable file of your python script, run the code given below-

Testing our executable

We should always test our executable in a new machine without any development environment like virtualev, conda etc. as the main aim of PyInstaller executable is that user should not require any thing installed on their system.

Now, after the running the executable file, the following error occurs.

This is because ‘importlib_resources’ requires version.txt file. We can add this file by the command given below.

This will include version.txt file in new folder in importlib_resources folder.

Conclusion

In this article, we learned how to install PyInstaller, run, debug and test our executable. Stay tuned!

Ссылка на основную публикацию