The python project packaged into exe executable files (multiple files and materials)

A. A single package file

1. Installation module Pyinstaller

2. Terminal Pycharm enter the command: "pyinstaller -F -w XXX.py" can produce exe.

In cmd: first cd to the project directory and then enter the command. For example: pyinstaller -F -w -ig: \ 2345Downloads \ logo.ico login.py

Notice: (g: \ 2345Downloads \ logo.ico) as an icon path.


/**
*附参数集合:

–icon=[图标路径](http://www.icontuku.com/)(pyinstaller -F --icon=my.ico XXXX.py)
-F 打包成一个exe文件
-w 小写,使用窗口,无控制台
-c 使用控制台,无窗口
-D 创建一个目录,里面包含exe以及其他一些依赖性文件 
-i  这里的i也是小写的,意思是忽略打包过程中遇到的错误,就是遇到错误也继续执行
*
*/


The resulting file in the same directory dist. Without -F parameter to generate a bunch of files, but running fast. After a single exe file compression ratio is also a little bit. Plus -F parameter to generate an exe file, and running slow.

II. The multiple files and packaging materials

Command: (package multiple files in cmd, the first primary file)

1 >pyinstaller -F -w -i g:\2345Downloads\logo.ico login.py

–hidden-import doTest.py --hidden-import itemContent --hidden-import queryDB.py

–hidden-import view.py --hidden-import Applications.py --hidden-import mainWindow.py

** explanation: cmd into the command line, then cd to the directory specific to our project code resides in the directory where the project code, using the code package.

There is no more placed in the project folder, some of the code files are scattered on the main folder, put after pyinstaller -F -w -i is the absolute path we need to finally display the icon, the first after the a Python file is the main file of the project, after using -hidden-import import some other Python files in this project.

If we put some specific features of the Python files in a specific folder, then we can use the -p folder name, for example:

We assume that we have created a sql folder to put some processing database files Python, assuming there are mysql.py **

Project contains material

1. This method can only be run on the machine

If the project contains a picture, we need to be referenced in the code of the picture becomes absolute path, not a relative path, an absolute path if not applicable, no error in the course of packaging, but also generate EXE files properly, but when we in the implementation of it, will prompt an error: fail to execute script login (here is the file name of your program entry in the file), reported after clicking the EXE file if you want to see the error, we can login directory. spec file console = True, the default is False, so then if set to True, will display an error message in the command line window. At the same time the black box to display the console if you do not want to open the program, the figure console = True change console = False

# -*- mode: python -*-
 
block_cipher = None
 
 
a = Analysis(['login.py'],
             pathex=['F:\\python_project_reveiw'],
             binaries=[],
             datas=[],
             hiddenimports=['doTest.py', 'itemContent', 'queryDB.py', 'view.py', 'Applications.py', 'mainWindow.py'],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='login',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          runtime_tmpdir=None,
          console=False , icon='g:\\2345Downloads\\logo.ico')

Once we have those parameters, we can file directly to login.spec packed with the following command: pyinstaller -F -w -ig: \ 2345Downloads \ logo.ico login.spec

Finally, the resulting EXE file will replace the previously generated EXE files.

Notice: 32-bit 64-bit operating system can not run the exe python packaged.

2. On any computer can run (picture)

1.Pic2py.py: picture into .py

import base64
 
def pic2py(picture_names, py_name):
    """
    将图像文件转换为py文件
    :param picture_name:
    :return:
    """
    write_data = []
    for picture_name in picture_names:
	    filename = picture_name.replace('.', '_')
	    open_pic = open("%s" % picture_name, 'rb')
	    b64str = base64.b64encode(open_pic.read())
	    open_pic.close()
	    # 注意这边b64str一定要加上.decode()
	    write_data.append('%s = "%s"\n' % (filename, b64str.decode()))

    f = open('%s.py' % py_name, 'w+')
    for data in write_data:
    	f.write(data)
    f.close()
 
if __name__ == '__main__':
    pics = ["icon.ico", "weixin.gif", "alipay.gif"]
    pic2py(pics, 'memory_pic')	 # 将pics里面的图片写到 memory_pic.py 中
    print("ok")

2 references in the program

from memory_pic import *		

def get_pic(pic_code, pic_name):
    image = open(pic_name, 'wb')
    image.write(b64decode(pic_code))
    image.close()

get_pic(icon_ico, 'icon_ico')
# 在这里使用图片 icon.ico
os.remove('icon.ico')

Packaged exe file is too large?

the reason

When you package the use pyinstaller .py file, it will allow many independent libraries installed while packed inside, resulting in generation of packaged .exe file size is too large.

Solution

Use pipenv create virtual python environment, install the library .py files used in a virtual environment (ie py file import third-party libraries), then use pyinstaller pip install the package used in the virtual environment created in pipenv. Note: The machine installed pyinstaller must also re-enter the virtual environment installed, or will use pyinstaller command packaging, or will generate an executable .exe file large volumes.

Specific steps are as follows:

1, pipenv pip command to install the library on the command line used to create a virtual environment

pip install pipenv

1
2. When ready to create a new environment file folder, and cd into the folder
3, the establishment of a virtual environment

pipenv install

. 1
4. enter the virtual environment (in the following operations are all Virtual Environment)

pipenv shell

. 1
5, the installation files used .py module

pip install [module] .py used

. 1
6, the mounting pyinstaller module, for packing document .py

pip install pyinstaller

1
7, using pyinstaller packaged file

pyinstaller -Fw E: \ pipenvtest \ test.py (.py file changes according to the actual situation)

8, the result file
after completing step 7 to run in the directory next time generate 'dist' folder, which is stored in the resulting file.
references

Public attention quickly [No.] easy to learn programming, learning resources receive it! (C, java, data structures, etc.)

Here Insert Picture Description

Published 50 original articles · won praise 8 · views 3055

Guess you like

Origin blog.csdn.net/jiahuan_/article/details/104631666