pyinstaller packaged! pyinstaller packaging path problem! The wordcloud package is missing the stopwords file

The wordcloud package is missing the stopwords file

wrong description

Unhandled exception in script
Failed to execute script'main' due to unhandled exception: (Errno 2] No such file or directory:
'C:\Users\VADMINI\appDatallLocal\Templ\2\ME186322\wordcloud\stopwords'

insert image description here

Solution

Find the path where wordcloud is installed.
By default, wordcloud.py in this folder of lib under your python installation path
insert image description here
is packaged with the following code

STOPWORDS = set(map(str.strip, open(os.path.join(FILE, 'stopwords')).readlines()))

changed to

STOPWORDS = set(map(str.strip, open(os.path.join(os.path.dirname(sys.executable), 'stopwords')).readlines()))

Change it to look for the stopwords file at the same level under the exe file when executing the exe program. You
can change the file location according to your own configuration requirements, as follows

STOPWORDS = set(map(str.strip, open(os.path.join(os.path.dirname(sys.executable), 'config','stopwords')).readlines()))

When executing the exe program, it will go to the same level directory under the exe file to find the stopwords file in the config directory

! ! ! You need to restore it when developing the environment, or make sure that your stopwords file can be found when running

pyinstaller packaging path problem

Python packs resource files together into exe to explain (with examples)

I directly use the following method for path mapping, and directly copy a copy of the configuration file in the project to the directory at the same level as the exe file

import os
import sys

def file_address():
    if getattr(sys, "frozen", False):
        BASE_DIR = os.path.dirname(sys.executable)
    else:
        BASE_DIR = os.path.dirname(os.path.abspath(__file__))

    return BASE_DIR

pyinstaller package

Libraries to be used

pip install pyinstaller

Single file packaging

cmd enters the py project directory and executes the command:

pyinstaller -F -w -i xxx.ico main.py --noconsole

or:

pyinstaller -F -w --icon=xxx.ico main.py --noconsole

The -F parameter means overlay packaging, so that when packaging, no matter how many times we package, it is the latest and fixed command.
-w means window program,
–icon is to set the display icon of exe, *.ico files can be processed by the online ico vector online conversion tool.
'main.py' is the entry point of the program (if it is a single file, replace it with the file name, such as: hello.py),
–noconsole means not to display the cmd window, and change it to –console if you want to see the cmd window.
Commonly used packaging commands:
packaging exe: pyinstaller -F main.py
packaging without console: pyinstaller -F -w main.py
packaging specified exe icon packaging: pyinstaller -F -i xx.ico main.py

Pack multiple files

cmd enters the py project directory and executes the command:

pyinstaller [主文件] -p [其他文件1] -p [其他文件2] --hidden-import [自建模块1] --hidden-import [自建模块2]

as follows:

pyinstaller -F -w --icon=testIcon.ico main.py -p addres.py -p test1.py -p test2.py --hidden-import addres --hidden-import test1 --hidden-import test2

Successfully packaged

insert image description here
Two folders will be generated in the root directory of the project.
insert image description here
There will be a xxx.exe in the dist folder. The name is named according to the main file .py you packaged. Double-click to execute it.
! ! ! If there is a configuration file, you need to first check the configuration file according to the directory structure of the development project and put it in the same directory as the exe.

It is said that using the Anaconda virtual environment can reduce the size of the exe file. It has not been used so far. If you are interested, you can explore it yourself.

Guess you like

Origin blog.csdn.net/god_sword_/article/details/130817025