[Details] python packaged exe file, pycharm+pyinstaller packaged successfully

Table of contents

Target

Configuration Environment

use pyinstaller

Bundled methods without resources 

The method of packaging py files and resources together

discuss 

reference


Target

1. Package the py files with resource files together into exe;

2. And the packaged file should be as small as possible.

Configuration Environment

If there is no limit to the size of the generated exe, you can use anaconda, and many unused packages will be entered together, which is not recommended. Use pure python+ to install the required packages, the typed exe will be much smaller, and it will be expanded in this way.

1. Install python, I am using win10+python3.7.0, python download address: Python Release Python 3.7.0 | Python.org The official home of the Python Programming Language https://www.python.org/downloads/release /python-370/

The interface is pulled to the bottom:

 Select the installation marked in the red box, and remember to check Add path to environment variables during installation, and the environment variables will be added automatically. If it is not checked but the installation is complete, don’t worry. For example, the directory where you installed python3.7.0 is in D:\pythonHome. Right-click My Computer->Advanced System Settings->Advanced->Environment Variables->Select Path under System Variables, double-click->New, enter D:\pythonHome, then create a new one, enter D:\pythonHome\Scripts, and it’s done Environment variable settings.

Set the pycharm environment to the newly installed python environment, refer to: pycharm modify the python environment_fenghutu's blog-CSDN blog_pycharm modify the python environment and enter the settings configuration environment interface If you can't find the python installation directory, click the button at 5 , after selecting the path to display the hidden folder, click OK https://blog.csdn.net/fenghutu/article/details/104281609

It is best to restart pycharm, then click on the terminal of pycharm, first enter python, and check whether the python is the newly installed python, if it is python of anaconda, there will be anaconda information in the prompt information, at this time, it is necessary to block it first Anaconda environment variables. Or directly press Shift + right mouse button in the D:\pythonHome directory to execute "Open the powershell window here", and the same is true for executing the packaging command in this place.

At the bottom of the Pycharm interface, you can see Terminal, select this option, this is a terminal interface.

insert image description here 

 

Write the input command in this interface: pip install pyinstaller to download pyinstaller.

During the download process, you may encounter some error messages. For solutions, please refer to my other blogs:
Common error message 1:
You are using pip version 10.0.1, however version 20.0.2 is available. You should consider upgrading via the 'python -m pip install --upgrade pip' command.
Common error prompt 2:
NoneType' object has no attribute 'bytes

Then it is to install some packages needed in the code.

use pyinstaller

Bundled methods without resources 

The command entered in the terminal is: pyinstaller -w -F XXX.py 

But for beginners, there are many things to explain here:
-w : Indicates that the cmd black box (that is, the black box in the picture) will not appear during the running of the generated .exe program (note: lowercase!)

-F : Indicates that you want to package all the programs together, and only one .exe file will be generated. Such a file is highly integrated, but the running speed is slow; if you do not write -F, there will be a bunch of .dll files generated, so There are many files in the program, but the running speed is relatively fast. This is also the style of the program we usually use (as shown in the figure) (note: capitalized!)

insert image description here

 XXX.py : It refers to the entry program of your entire project. When you write a project, it is likely to be multi-file programming. When you use which file as the entry to pull up your entire project, fill in the name of that file.

After running the pyinstaller -w -F XXX.py command, two folders will be generated: one is called build and the other is called dist.

insert image description here 

Click to open the dist folder, there will be a folder named after the XXX you filled in, and then click in, you will see the XXX.exe file.

 insert image description here

At this point, if there are no other resource files that need to be packaged together except the py file, theoretically you have generated the .exe executable file, and this is done. But if you need to package resource files together, you need to use another method.

The method of packaging py files and resources together

The method used here is to modify the generated .spec file, but it is wrong to check many blogs, and it has been proved to be possible after modification.

In your own code, add the following function first, which is to convert the writing method with resource path into the official writing method:

#coding:utf-8
import sys
import os

#生成资源文件目录访问路径
def resource_path(relative_path):
    if getattr(sys, 'frozen', False): #是否Bundle Resource
        base_path = sys._MEIPASS
    else:
        base_path = os.path.abspath(".")
    return os.path.join(base_path, relative_path)

#访问res文件夹下a.txt的内容
filename = resource_path(os.path.join("res","a.txt"))

fielname is the subpath you need to call the resource, for example, the address of the resource is G:\temp\res\ a.txt, the path of filename can be at the level of res, and the root directory will be written in the .spec file later .

The difference from the method without resources is that you need to execute the command to generate the .spec file first, pyi-makespec -F test.py (if you want to add Icon, etc., you can use pyi-makespec --icon abc.jpg -F test. py statement to generate a spec file). Add the root directory in the .spec file pathex, and add resources in datas.

Finally, just execute pyinstaller xxx.spec , xxx.spec is the file just modified. At this point, the process of packaging with resources is complete.

Common error prompt one:

ImportError: cannot import name ‘PackagePath‘ from ‘importlib_metadata‘

Common error prompt two: 

Pyinstaller package exe appears PermissionError: [Errno 13] Permission denied: 'C:\\...\xxx.dll

discuss 

If the message is not answered in time, you can join the group discussion:

 

reference

How does Python generate exe files? Use Pycharm to take you to learn step by step (super detailed, super intimate)_Nire_Yeyu's blog-CSDN blog_python to generate exe files

Download pyinstaller error: You should consider upgrading via the 'python -m pip install --upgrade pip' command._Nire_Yeyu's Blog-CSDN Blog

Pycharm error: 'NoneType' object has no attribute 'bytes'_Nire_Yeyu's Blog-CSDN Blog 

PyInstaller solves ImportError: cannot import name 'PackagePath' from 'importlib_metadata'_小白tree's blog-CSDN blog  Pyinstaller package exe appears PermissionError: [Errno 13] Permission denied: 'C:\\...\xxx.dll_zhf's blog -CSDN blog

How does Pyinstaller package resource files together into exe - darcymei - 博客园 (the method is wrong)

Guess you like

Origin blog.csdn.net/qq_36076233/article/details/122960071