pandas_datareader,导入错误:无法导入名称“urlencode”

我使用 pandas_datareader 工作得很好,然后今天我从下面的链接安装了雅虎财经,试图解决另一个问题。

未获取数据 Web.DataReader Panda

pip install yfinance

pip install fix_yahoo_finance

上述安装完成后,pandas_datareader就不能再使用了。我用谷歌搜索了它,并添加了以下导入,但 pandas_datareader 仍然无法工作。


from urllib.parse import urlencode

这是错误:非常感谢您的帮助。


from pandas_datareader import data


  File "C:\Users\yongn\Anaconda3\lib\site-packages\pandas_datareader\__init__.py", line 2, in <module>

    from .data import (

  File "C:\Users\yongn\Anaconda3\lib\site-packages\pandas_datareader\data.py", line 11, in <module>

    from pandas_datareader.av.forex import AVForexReader

  File "C:\Users\yongn\Anaconda3\lib\site-packages\pandas_datareader\av\__init__.py", line 6, in <module>

    from pandas_datareader.base import _BaseReader

  File "C:\Users\yongn\Anaconda3\lib\site-packages\pandas_datareader\base.py", line 7, in <module>

    from pandas.io.common import urlencode

ImportError: cannot import name 'urlencode'

'''


慕虎7371278
浏览 370回答 4
4回答

紫衣仙女

好的,我通过升级 pandas datareader 解决了这个问题pip install pandas-datareader --upgrade``Thanks

青春有我

我遇到了完全相同的错误。我正在使用 python anaconda 2020_07 版本。解决方案是使用 anaconda 包中最新的 pandas-datareader v0.9。如果您使用 conda-forge 中的 pandas-datareader 包(使用旧版本 v0.81),您将遇到该错误。这是截至 2020 年 12 月 20 日的状态。我运行下面的命令来安装最新的pandas-datareader软件包。conda install -c anaconda pandas-datareader错误消息消失,问题已得到解决。编辑:如果 conda 稍后降级pandas-datareader回 conda-forge 旧版本,则有修复。

跃然一笑

原因是 pandas 从他们的库中删除了 urlencode。因此,对于较新版本的 pandas 来说,这永远不会起作用。安装其他库或升级无法解决该问题。修复方法是使用 Python3 版本的 urlencode。幸运的是,Python3 的替代品似乎有所下降:替换这个:from pandas.io.common import urlencode和:from urllib.parse import urlencode并像往常一样使用 urlencode

眼眸繁星

上面的答案是正确的。我刚刚编写了一些实现它的代码:import osbasePath = os.path.join(os.path.dirname(os.__file__),'site-packages','pandas_datareader','base.py')# read base.pywith open(basePath, 'r') as f:&nbsp; &nbsp; lines = f.read()find = 'from pandas.io.common import urlencode'replace = """from urllib.parse import urlencode"""# add new textlines = lines.replace(find,replace)# overwrite old 'basedatatypes.py'with open(basePath, 'w') as f:&nbsp; &nbsp;&nbsp; &nbsp; f.write(lines)initPath = os.path.join(os.path.dirname(os.__file__),'site-packages','pandas_datareader','iex','__init__.py')# read iex/__init__.pywith open(initPath, 'r') as f:&nbsp; &nbsp; lines = f.read()# add new textlines = lines.replace(find,replace)# overwrite old 'basedatatypes.py'with open(initPath, 'w') as f:&nbsp; &nbsp;&nbsp; &nbsp; f.write(lines)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python