首页 > 解决方案 > 在 conda 上运行 OSMnx 的问题

问题描述

我试图让 Python 包 OSMnx 在我的 Windows10 机器上运行。我还是 python 的新手,所以在基础知识上苦苦挣扎。我已按照此处的说明https://osmnx.readthedocs.io/en/stable/并成功创建了一个新的 conda 环境以供其运行。安装似乎一切正常。但是,一旦我尝试导入它,我就会收到以下错误

>>> import osmnx as ox
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\User\.conda\envs\ox\lib\site-packages\osmnx\__init__.py", line 3, in <module>
    from ._api import *
  File "C:\Users\User\.conda\envs\ox\lib\site-packages\osmnx\_api.py", line 4, in <module>
    from .distance import get_nearest_edge
  File "C:\Users\User\.conda\envs\ox\lib\site-packages\osmnx\distance.py", line 5, in <module>
    import networkx as nx
  File "C:\Users\User\.conda\envs\ox\lib\site-packages\networkx\__init__.py", line 114, in <module>
    import networkx.generators
  File "C:\Users\User\.conda\envs\ox\lib\site-packages\networkx\generators\__init__.py", line 14, in <module>
    from networkx.generators.intersection import *
  File "C:\Users\User\.conda\envs\ox\lib\site-packages\networkx\generators\intersection.py", line 13, in <module>
    from networkx.algorithms import bipartite
  File "C:\Users\User\.conda\envs\ox\lib\site-packages\networkx\algorithms\__init__.py", line 16, in <module>
    from networkx.algorithms.dag import *
  File "C:\Users\User\.conda\envs\ox\lib\site-packages\networkx\algorithms\dag.py", line 23, in <module>
    from fractions import gcd
ImportError: cannot import name 'gcd' from 'fractions' (C:\Users\User\.conda\envs\ox\lib\fractions.py)

我正在运行

   conda version : 4.8.2
   conda-build version : 3.18.11
   python version : 3.7.6.final.0

有人能给我建议吗?抱歉,如果这很明显,正如我所说,我对这一切都很陌生。谢谢

标签: pythoncondaosmnx

解决方案


该模块fractions是 Python 标准库的一部分。曾经有一个 functiongcd,正如链接文档所说,它是:

3.5 版后已弃用:math.gcd()改为使用。

由于该函数gcd已从 Python 3.9 中的模块fractions中删除,因此该问题似乎使用 Python 3.9,而不是问题说明的 Python 3.7.6,因为该 Python 版本仍然具有fractions.gcd.

该错误由 引发networkx。升级到最新版本networkx有望避免此问题:

pip install -U networkx

Indeed, the change that avoids this error from networkxis: https://github.com/networkx/networkx/commit/b007158f3bfbcf77c52e4b39a81061914788ddf9#diff-21e03bb1d46583650bcad6e960f2ab8a5397395c986942b59314033e963dd3fcL23 , and has been released as part of networkx==2.4, networkx==2.5, and networkx==2.5.1, as the tags listed on the commit's GitHub页面通知。当前行networkx是:https ://github.com/networkx/networkx/blob/d70b314b37168f0ea7c5b0d7f9ff61d73232747b/networkx/algorithms/dag.py#L9 ,即from math import gcd


推荐阅读