conda 上的问题 运行 OSMnx

Issues running OSMnx on conda

我正在尝试在我的 Windows10 机器上获取 Python 软件包 OSMnx 运行ning。我对 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

有人能给我建议吗?很抱歉,如果这很明显,正如我所说的,我是这一切的新手。谢谢

模块 fractions is part of the Python standard library. There used to be a function gcd,如链接文档所述,它是:

Deprecated since version 3.5: Use math.gcd() instead.

由于函数gcd在Python 3.9中被从模块fractions中移除,看来问题使用的是Python 3.9,而不是Python 3.7.6 作为问题注释,因为 Python 版本 still had fractions.gcd.

错误是由 networkx 引发的。升级到最新版本的networkx有望避免这个问题:

pip install -U networkx

确实,从 networkx 中避免此错误的更改是: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 page inform. The current line in networkx is: https://github.com/networkx/networkx/blob/d70b314b37168f0ea7c5b0d7f9ff61d73232747b/networkx/algorithms/dag.py#L9,即 from math import gcd.

我遇到了同样的问题。我使用 conda-forge 安装了所有软件包(推荐安装 OSMnx - https://osmnx.readthedocs.io/en/stable/)。如果我理解正确的话,Conda-forge 不支持包更新。相反,删除 networkx 并重新安装,但一定要指定版本。您也需要重新安装 osmnx

conda remove -c conda-forge networkx
conda install -c conda-forge networkx=2.5
conda install -c conda-forge osmnx