ISO3166 not importing method properly -Pycharm

264 views Asked by At

Anyone know why this is not working properly. According to https://pypi.org/project/iso3166/ this should work.

from iso3166 import countries

Get following error:

ImportError: cannot import name 'countries' from 'iso3166'

2

There are 2 answers

0
Thomas Weinandy On

I ran into this same issue which stemmed using an in-line pip install from my Jupyter notebook. Here a few common solutions worth trying:

  • Update the package
  • Uninstall and reinstall the package
  • Restart your kernel (if in a notebook)

Here is what worked for me:

  1. Manually deleted the library from my environment (specifically, by the file path displayed with the ImportError message)
  2. Reinstalled the library using my IDE's library manager

If that still doesn't work for you, consider trying a different library, like this one: https://pypi.org/project/pycountry/

0
Tobias Herrmann On

You can also use countrywrangler which should also be a bit faster.

pip install countrywrangler

Here is an example code:

import countrywrangler as cw

alpha2 = cw.Normalize.name_to_alpha2("Germany")
print(alpha2)

>>> DE

There is also a fuzzy option that matches virtually any different formatting:

import countrywrangler as cw

alpha2 = cw.Normalize.name_to_alpha2("Germany Federal Republic of", use_fuzzy=True)
print(alpha2)

>>> DE

https://pypi.org/project/countrywrangler/

https://countrywrangler.readthedocs.io/en/latest/