
楼主非专业 python 开发,今天遇到个完全没头脑的问题,请赐教!.
geoip2.database 模块内一个全局变量 read = geoip2.database.Reader(geoipfile)geoipfile 没影响...这是咋回事啊??... import unittest from .geoip import location class Test_lib(unittest.TestCase): def test_locations(self): ips = ['23.250.80.1', '24.199.128.3', '31.173.152.1', '186.115.160.2'] adds = locations(ips) self.assertEqual(len(adds), 4) #!/usr/bin/python3 import geoip2.database, logging geoipfile = "./GeoLite2-City.mmdb" read = geoip2.database.Reader(geoipfile) def locations(ips: list[str]) -> list: s = location() next(s) return [s.send(x) for x in ips] def location() -> list: ip = "1.0.0.1" while True: try: respOnse= read.city(ip) a = [ip, response.location.longitude, response.location.latitude] except Exception as e: logging.exception(e) a = [] ip = yield a 1 simple2025 Jul 4, 2021 ``` from .geoip import location ``` 这个改成绝对路劲看一下? |
2 JasperHale OP @chenqh 改成绝对路径也不行. 无论是绝对路径还是相对路径,都找不到测试文件... 只要 `#read = geoip2.database.Reader(geoipfile)` 注释掉 read 马上就能找到..的 |
3 JasperHale OP @chenqh 就是我把其他都去掉,新建一个 .py,文件 只有 `read = geoip2.database.Reader(geoipfile)`, 测试文件只要 from import 或者 import 就找不到测试文件.... |
4 simple2025 Jul 4, 2021 |
5 learningman Jul 5, 2021 via Android 换个变量名?怕不是撞了 |
6 no1xsyzy Jul 5, 2021 报了没有顶级包不能相对路径 报了 adds = locations(ips) 中 locations 不存在 - from .geoip import location + from geoip import locations 除此以外,我这边正常 |
7 JasperHale OP |
8 JasperHale OP |
9 JasperHale OP @learningman 确实是变量名的问题,谢谢,已解决 |