maxmind / GeoIP2-python

Python code for GeoIP2 webservice client and database reader
https://geoip2.readthedocs.org/en/latest/
Apache License 2.0
1.11k stars 141 forks source link

pathlib.Path support #114

Closed fisher85 closed 3 years ago

fisher85 commented 3 years ago

I try to construct Reader with pathlib.Path object:

from geoip2.database import Reader
from pathlib import Path

# db_path = '/tmp/GeoLite2-Country.mmdb'
db_path = Path('/tmp') / 'GeoLite2-Country.mmdb'

reader = Reader(db_path, locales=['en'])
print(reader.country('8.8.8.8').country.name)

And I get the error:

Traceback (most recent call last):
  File "/home/xxx/test/1.py", line 6, in <module>
    reader = Reader(db_path, locales=['en'])
  File "/usr/lib/python3.9/site-packages/geoip2/database.py", line 106, in __init__
    self._db_reader = maxminddb.open_database(fileish, mode)
  File "/usr/lib64/python3.9/site-packages/maxminddb/__init__.py", line 48, in open_database
    return maxminddb.extension.Reader(database)
TypeError: argument 1 must be str, not PosixPath

The solution to the problem is to use str(db_path): reader = Reader(str(db_path), locales=['en'])

However, it would be great if the GeoIP2-python package would support the pathlib.Path objects.

oschwald commented 3 years ago

This makes sense. We should accept os.PathLike and then use os.fspath to convert it when necessary.

oschwald commented 3 years ago

This is fixed by https://github.com/maxmind/MaxMind-DB-Reader-python/pull/85.

oschwald commented 3 years ago

The fix for this has been released.