V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Yycreater
V2EX  ›  Python

想要用 Python 的 pygeoip 库通过 ip 获取经纬,然后给百度地图,在页面显示,结果在形成对象这儿卡住了 各位大佬,帮吗看一下是啥情况?

  •  
  •   Yycreater ·
    Yycreater · 2018-07-26 19:13:11 +08:00 · 2678 次点击
    这是一个创建于 2094 天前的主题,其中的信息可能已经有所发展或是发生改变。
    import pygeoip
    gi = pygeoip.GeoIP('GeoLiteCity.dat')
    response = gi.record_by_addr(ip)
    我正常导入库,路径也正确,想要用 Python 的 pygeoip 库通过 ip 获取经纬,然后给百度地图,在页面显示,结果在形成对象这儿卡住了
    各位大佬,帮吗看一下是啥情况?

    分割线

    File "/usr/local/lib/python2.7/site-packages/pygeoip/__init__.py", line 118, in __init__
    self._fp = codecs.open(filename, 'rb', ENCODING)
    File "/usr/local/lib/python2.7/codecs.py", line 896, in open
    ------------if mode[:1] not in set('rwa'):
    --------------mode = 'r' + mode
    Open an interactive python shell in this frame if 'b' not in mode:
    ------------# Force opening of the file in binary mode
    ------------mode = mode + 'b'
    ----file = __builtin__.open(filename, mode, buffering) # 报这个错误!!!
    ----if encoding is None:
    --------return file
    ----info = lookup(encoding)
    ----srw = StreamReaderWriter(file, info.streamreader, info.streamwriter, errors)
    ----# Add attributes to simplify introspection
    IOError: [Errno 2] No such file or directory: 'GeoLiteCity.dat'
    6 条回复    2018-07-27 09:48:56 +08:00
    xiexingjia
        1
    xiexingjia  
       2018-07-26 19:20:09 +08:00
    No such file or directory: 'GeoLiteCity.dat'
    Yycreater
        2
    Yycreater  
    OP
       2018-07-26 19:38:06 +08:00
    文件我直接放在同级目录下了,文件路径没问题,刚刚在项目外面试了一下{'city': u'Shenzhen', 'region_code': u'30', 'area_code': 0, 'time_zone': 'Asia/Chongqing', 'dma_code': 0, 'metro_code': None, 'country_code3': 'CHN', 'latitude': 22.533299999999997, 'postal_code': None, 'longitude': 114.13330000000002, 'country_code': 'CN', 'country_name': 'China', 'continent': 'AS'}
    很 ok !项目内就是不行。。。。
    KeatingSmith
        3
    KeatingSmith  
       2018-07-26 19:42:51 +08:00 via iPhone
    使用绝对路径吧。

    有可能你的 Python 运行路径与文件路径不一致,而你又使用的是相对路径
    Yycreater
        4
    Yycreater  
    OP
       2018-07-26 19:52:52 +08:00
    试了,还是报那个错误~~~
    lieh222
        5
    lieh222  
       2018-07-27 08:49:20 +08:00   ❤️ 1
    print(os.getcwd())
    self._fp = codecs.open(filename, 'rb', ENCODING)
    您瞧瞧打印的当前路径对不对
    Yycreater
        6
    Yycreater  
    OP
       2018-07-27 09:48:56 +08:00
    @lieh222 托您的福,用 print os.path.realpath(__file__)
    print '*' * 50 方法调好了路径
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1014 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 18:53 · PVG 02:53 · LAX 11:53 · JFK 14:53
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.