countrycode package TypeError: 'module' object is not callable – Error shows wrong temp file: AppDataLocalTempipykernel_

Question:

I know this has been asked elsewhere, but none of the solutions seem to be working.

I am trying to call the countrycode package (https://pypi.org/project/countrycode/).

Here is some test code:

from countrycode import countrycode
countrycode(codes=['Algeria', 'United States'], origin='country_name', target='iso3c')

Traceback (most recent call last):

  File "C:UsersNAMEAppDataLocalTempipykernel_260922392981478.py", line 2, in <module>
    countrycode(codes=['Algeria', 'United States'], origin='country_name', target='iso3c')

TypeError: 'module' object is not callable

I see that the module call seems to be pointing at "C:UsersNAMEAppDataLocalTempipykernel_260922392981478.py". When I look for this file on my system, I can’t find it.

Searching around I see that some people have had problems with Jupyter notebooks setting the working directory as a temp folder that has a similar path: Current working directory for jupyter notebook sets to temp folder in vscode

I am using Spyder, running it within Anaconda – via which I installed countrycode using pip3.

I can’t work out what the issue is. os.getcwd() returns the correct working directory. Further print(countrycode) appears to point to the correct module location: <module 'countrycode.countrycode' from 'C:\Users\NAME\Anaconda3\lib\site-packages\countrycode\countrycode.py'>. When I look at that countrycode.py file it seems to contain all the correct information.

I am assuming that somewhere some variable got created (in another script nothing here is named countrycode, and I have restarted) that is pointing to the Temp folder. But I can’t work out how to make the module call point to the correct place – especially when the print call seems to do so.

Asked By: MorrisseyJ

||

Answers:

It looks like this package is not well supported, and its documentation on PyPI is inaccurate. I was able to get this working by updating the import statement to look one level deeper in the package:

from countrycode.countrycode import countrycode

print(
  countrycode(
    codes=['Algeria', 'United States'], 
    origin='country_name', target='iso3c'
  )
)

Unfortunately the GitHub for this package does not appear to still be live, so I cannot provide with certainty further details as to why the additional layer works.

Answered By: Ethan Cloin
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.