Use pyinstaller packaged Python project contains multiple folders

  Recent use Python to write a wife, two small projects, to solve her repeated every day some manual operations. Wife very happy, but the question is, she is a Python white, for her, you need to install and configure the Python environment is a lot of third-party modules troublesome thing. And the follow-up to the transfer of work to other people, first, we need to re-install the Python environment, and second, I worked hard for her to write source code so exposed.

  To solve this problem, so we began to Baidu. Python indeed still a lot of encrypted source method, and may be packaged into exe executable file. This not only protects the source of security, but also eliminates the cumbersome installation of Python environment.

The beginning is tried py2exe this module, it took two hours, or did not shift the success of Baidu checked py2exe say not very good, and I determined to give up, try another way. Then there pyinstaller experience. There are a lot of documentation on Baidu, but still have to go to their real job again operational. Then we started.

  The first is to install pyinstaller module, this is very simple, direct pip install pyinstaller on the line. We will talk about how to use it. Start with the most simple start. . . .

  Write a simple script holle.py, why at the end of the script added time.splee (5)? Because the generated exe file execution Tete so quickly, I did not see what was printed on the window automatically closes docs.

# -*- coding: utf-8 -*-
#2019/12/25 14:24

import time
print('#######################################################################################')
print('#######################################################################################')
with open('123.txt','r') as f:
    ret = f.read()
    print(ret)

print('#######################################################################################')
print('#######################################################################################')
print('#######################################################################################')
time.sleep(5)
hello.py

Here a brief two parameters pyinstaller use:

:-D parameters to generate a file directory contains the executable files and dynamic link libraries and resource-related documents; -F only generate an executable exe file.

-D, --onedir Create a one-folder bundle containing an executable (default)
-F, --onefile Create a one-file bundled executable。


Open a terminal, go to the directory where hello.py, do pyinstaller -F hello.py. Executing the automatically generated build, dist directory and hello.spec file.

 

 

咱要的exe文件在dist目录下,其他的就没有用了。直接把dist目录下的 hello.exe文件拷贝到其他地方,比如桌面,哈哈。然后在桌面创建一个123.txt文件,随便写点什么。主要测试一下exe能否正常执行,有没有打印预期的内容。试了一下,不错。是预期的效果。那接下来就可以开始我们的正题了。打包多个目录下的多个py文件。

代码目录结构如下:

 

 

 

1、先生成spec文件

 打开Terminal,进入到bin目录下,执行pyinstaller -D run.py,还是一样执行完会自动生成build、dist目录和run.spec文件。让我们来看看spec文件长啥样。

 

 

 2、配置spec文件

因为项目里写了好多自定义的模块,以及图片模板文件。所以需要配置一下spec文件,才能正确的生成exe文件。

主要配置以下4个地方即可:

 

 

 

(1) py文件打包配置

  针对多目录多文件的python项目,打包时候需要将所有相关的py文件输入到Analysis类里。Analysis类中的pathex定义了打包的主目录,对于在此目录下的py文件可以只写文件名不写路径。如上的spec脚本,将所有项目中的py文件路径以列表形式写入Analysis,这里为了说明混合使用了绝对路径和相对路径。

(2) 资源文件打包配置

  资源文件包括打包的python项目使用的相关文件,如图标文件,文本文件等。对于此类资源文件的打包需要设置Analysis的datas

比如:datas=[(SETUP_DIR + '\\db\\input','db\\input'),(SETUP_DIR + '\\doc','doc')],只需要添加依赖的输入目录以及放图片、模板的目录即可。
注意:SETUP_DIR + '\\db\\input'为源码中的绝对路径,Windows路径需要用\\来转义,后面的db\\input为打包后生成的目录,写相对路径即可。这样我们就不需要再手动把依赖的文件拷贝到run目录了

(3)Hidden import配置

  pyinstaller在进行打包时,会解析打包的python文件,自动寻找py源文件的依赖模块。但是pyinstaller解析模块时可能会遗漏某些模块(not visible to the analysis phase),造成打包后执行程序时出现类似No Module named xxx。这时我们就需要在Analysis下hiddenimports中加入遗漏的模块。
如:

from core.barcode_handler import *
from conf import settings as ss
from core.log import Log as log
只需要写入自定义的模块即可
hiddenimports=['core','core.main','conf.settings','core.log','core.barcode_handler']

 

 

 

Guess you like

Origin www.cnblogs.com/hulk-1029/p/12106630.html