PyInstaller 不适用于模块 pycountry?

PyInstaller not working with module pycountry?

通常 PyInstaller 对我来说工作正常,但我发现使用 python-module 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

是否有任何解决方法可以让 运行 pyinstaller 获得 pycountry 功能?

更新: 为我的问题找到了解决方法/解决方案-

  1. 使用命令 生成一个 temp2.spec 文件
  2. change filename.spec => from PyInstaller.utils.hooks import copy_metadata (in the header) => in the a = Analysis(...) section change " datas = []," 至
  3. 如上使用pyinstaller编译exe

备选方案:先编译程序 - 更改规范 - 使用命令 – Rapid1898 刚刚编辑

你的更新对我很有帮助。但是我需要一个变体才能让它工作。我只想让这些想法更清楚地展示 pycountry 库正常工作所必须完成的方式。

  1. 使用命令pyi-makespec --onefile name-of-your-file.py生成.spec文件 名称 name-of-your-file.spec
  2. 使用您喜欢的文本编辑器打开 name-of-your-file.spec。
    • 将以下行添加到 .spec 文件的顶部 from PyInstaller.utils.hooks import copy_metadata
    • datas = []替换为datas = copy_metadata("pycountry")
  3. 然后使用以下命令重建PyInstaller --clean name-of-your-file.spec

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

您可以阅读 using spec files 的文档。

你可以在这个 link.

中找到相同的过程