PyInstaller不能与pycountry模块一起正常工作?

4
通常情况下,PyInstaller 对我来说效果良好,但我遇到了一个使用 python 模块 pycountry 的问题。
我尝试了这个非常简单的代码:
import pycountry
land="DE"
country = pycountry.countries.get (alpha_2=land)
print(country.name)

使用pyinstaller进行编译:

pyinstaller --onefile xyz.py

但是当我想要执行编译后的exe文件时,出现了以下错误:

Traceback (most recent call last):
  File "temp2.py", line 1, in <module>
    import pycountry
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "c:\users\polzi\appdata\local\programs\python\python37\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages\pycountry\__init__.py", line 12, in <module>
  File "site-packages\pkg_resources\__init__.py", line 481, in get_distribution
  File "site-packages\pkg_resources\__init__.py", line 357, in get_provider
  File "site-packages\pkg_resources\__init__.py", line 900, in require
  File "site-packages\pkg_resources\__init__.py", line 786, in resolve
pkg_resources.DistributionNotFound: The 'pycountry' distribution was not found and is required by the application
[45548] Failed to execute script temp2

有没有什么解决办法可以让pycountry功能在pyinstaller中运行?

更新:我找到了一个解决方案 —

  1. 使用命令 <pyi-makespec --onefile temp2.py> 生成temp2.spec文件
  2. 更改filename.spec => from PyInstaller.utils.hooks import copy_metadata(在头文件中)=> 在a = Analysis(...)部分将"datas = []," 更改为 <datas = copy_metadata("pycountry"),>
  3. 使用pyinstaller编译exe如上

另一种方法:在编译程序之前更改spec文件并使用命令 <pyInstaller --clean temp2.spec> - Rapid1898刚才编辑过

1个回答

5

你的更新对我非常有帮助。但是为了让它能够正常工作,我需要做一些变化。 我只需要按照以下方式排序这些想法,以更清晰地显示必须执行的步骤,以使 pycountry 库正常工作。

  1. 使用命令 pyi-makespec --onefile 你的文件名.py 生成一个名为 你的文件名.spec 的 .spec 文件。
  2. 用你喜欢的文本编辑器打开 你的文件名.spec。
    • 在 .spec 文件顶部添加以下行:from PyInstaller.utils.hooks import copy_metadata
    • datas = [] 替换为 datas = copy_metadata("pycountry")
  3. 然后使用以下命令重新构建:PyInstaller --clean 你的文件名.spec

我希望对那些遇到同样错误的人有所帮助。

您可以阅读以下文档有关使用 spec 文件的说明.

您还可以在此链接中找到相同的过程。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接