Rapid1898
Rapid1898

Reputation: 1190

PyInstaller not working with module pycountry?

Normally PyInstaller works fine for me but i saw a problem using the python-module pycountry.

I tried this very simple code:

import pycountry
land="DE"
country = pycountry.countries.get (alpha_2=land)
print(country.name)

Compiled it with pyinstaller:

pyinstaller --onefile xyz.py

But i want to execute the compiled exe i get this error:

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

Is there any workaround i can get pycountry functionality to running with pyinstaller?

UPDATE: found a workaround / solution for my problem-

  1. use command <pyi-makespec --onefile temp2.py> to generate a temp2.spec file
  2. change filename.spec => from PyInstaller.utils.hooks import copy_metadata (in the header) => in the a = Analysis(...) section change " datas = []," to <datas = copy_metadata("pycountry"),>
  3. use pyinstaller to compile exe as above

Alternative: compile program before - change spec - an use command <pyInstaller --clean temp2.spec> – Rapid1898 just now Edit

Upvotes: 4

Views: 1173

Answers (1)

Kaylee Paez
Kaylee Paez

Reputation: 118

Your update was really helpful to me. But I needed a variation in order for it to work. I would just order the ideas to show clearer the way it must be done for pycountry library to work properly.

  1. Use command pyi-makespec --onefile name-of-your-file.py to generate a .spec file with name name-of-your-file.spec
  2. Open name-of-your-file.spec with the text editor of your preference.
    • Add to the top of the .spec file the following line from PyInstaller.utils.hooks import copy_metadata
    • Replace datas = [] for datas = copy_metadata("pycountry")
  3. Then rebuild using the following command PyInstaller --clean name-of-your-file.spec

I hope it is helpful for anyone facing the same error.

You can read the documentation for using spec files.

And you can find the same process in this link.

Upvotes: 5

Related Questions