最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Mac中Python 3.8导入tushare报错:ImportError: cannot import name StringIO from pandas.compat

Mac crifan 2709浏览 0评论
折腾:
【未解决】mac中pip安装Python库tushare
期间,Mac中,Python 3.8中,去导入tushare报错:
⚙ limao@xxx  ~  python
Python 3.8.0 (default, Jan  6 2020, 10:33:50)
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tushare
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/limao/.pyenv/versions/3.8.0/Python.framework/Versions/3.8/lib/python3.8/site-packages/tushare/__init__.py", line 11, in <module>
    from tushare.stock.trading import (get_hist_data, get_tick_data,
  File "/Users/limao/.pyenv/versions/3.8.0/Python.framework/Versions/3.8/lib/python3.8/site-packages/tushare/stock/trading.py", line 24, in <module>
    from tushare.stock.fundamental import get_stock_basics
  File "/Users/limao/.pyenv/versions/3.8.0/Python.framework/Versions/3.8/lib/python3.8/site-packages/tushare/stock/fundamental.py", line 19, in <module>
    from pandas.compat import StringIO
ImportError: cannot import name 'StringIO' from 'pandas.compat' (/Users/limao/.pyenv/versions/3.8.0/Python.framework/Versions/3.8/lib/python3.8/site-packages/pandas/compat/__init__.py)
>>>
重新安装:
 ✘ ⚙ limao@xxx  ~  pip reinstall tushare
ERROR: unknown command "reinstall" - maybe you meant "install"
 ✘ ⚙ limao@xxx  ~  pip uninstall tushare
Uninstalling tushare-1.2.48:
  Would remove:
    /Users/limao/.pyenv/versions/3.8.0/Python.framework/Versions/3.8/lib/python3.8/site-packages/pytdx/*
    /Users/limao/.pyenv/versions/3.8.0/Python.framework/Versions/3.8/lib/python3.8/site-packages/test/*
    /Users/limao/.pyenv/versions/3.8.0/Python.framework/Versions/3.8/lib/python3.8/site-packages/tushare-1.2.48.dist-info/*
    /Users/limao/.pyenv/versions/3.8.0/Python.framework/Versions/3.8/lib/python3.8/site-packages/tushare/*
Proceed (y/n)? y
  Successfully uninstalled tushare-1.2.48
 ⚙ limao@xxx  ~  pip install tushare
Collecting tushare
  Using cached https://files.pythonhosted.org/packages/bf/3f/149ef2a80cddd24410e6788228a22f3b3afb5c55c765a223a5c7ecf93ecf/tushare-1.2.48-py3-none-any.whl
Installing collected packages: tushare
Successfully installed tushare-1.2.48
结果:
问题依旧。
tushare ImportError: cannot import name StringIO from pandas.compat
Cannot import name ‘StringIO’ from ‘pandas.compat‘ · Issue #655 · pydata/pandas-datareader · GitHub
https://github.com/pydata/pandas-datareader/issues/655
说是降低版本:
pip3 install --upgrade pandas==0.24.2
pipenv install pandas~=0.24.2
或:
pip install pandas-datareader --upgrade
此处已安装pandas版本是
 pip show pandas
Name: pandas
Version: 1.0.0
Summary: Powerful data structures for data analysis, time series, and statistics
Home-page: https://pandas.pydata.org
Author: None
Author-email: None
License: BSD
Location: /Users/limao/.pyenv/versions/3.8.0/Python.framework/Versions/3.8/lib/python3.8/site-packages
Requires: numpy, pytz, python-dateutil
Required-by:
1.0.0
python – How to fix “ImportError: cannot import name ‘StringIO'” – Stack Overflow
也是:
pip install "pandas<0.25.0"
pipenv install "pandas<0.25.0"
去试试
pip install --upgrade pandas==0.24.2
速度太慢,终止:
 pip install --upgrade pandas==0.24.2
Collecting pandas==0.24.2
  Downloading https://files.pythonhosted.org/packages/b2/4c/b6f966ac91c5670ba4ef0b0b5613b5379e3c7abdfad4e7b89a87d73bae13/pandas-0.24.2.tar.gz (11.8MB)
     |▌                               | 194kB 7.6kB/s eta 0:25:26^C
ERROR: Operation cancelled by user
此处加代理去加速下载
pip --proxy http://127.0.0.1:1087 install --upgrade pandas==0.24.2
log
 ✘ ⚙ limao@xx  ~  pip --proxy http://127.0.0.1:1087 install --upgrade pandas==0.24.2
Collecting pandas==0.24.2
  Downloading https://files.pythonhosted.org/packages/b2/4c/b6f966ac91c5670ba4ef0b0b5613b5379e3c7abdfad4e7b89a87d73bae13/pandas-0.24.2.tar.gz (11.8MB)
     |████████████████████████████████| 11.8MB 171kB/s
Requirement already satisfied, skipping upgrade: python-dateutil>=2.5.0 in ./.pyenv/versions/3.8.0/Python.framework/Versions/3.8/lib/python3.8/site-packages (from pandas==0.24.2) (2.8.1)
Requirement already satisfied, skipping upgrade: pytz>=2011k in ./.pyenv/versions/3.8.0/Python.framework/Versions/3.8/lib/python3.8/site-packages (from pandas==0.24.2) (2019.3)
Requirement already satisfied, skipping upgrade: numpy>=1.12.0 in ./.pyenv/versions/3.8.0/Python.framework/Versions/3.8/lib/python3.8/site-packages (from pandas==0.24.2) (1.18.1)
Requirement already satisfied, skipping upgrade: six>=1.5 in ./.pyenv/versions/3.8.0/Python.framework/Versions/3.8/lib/python3.8/site-packages (from python-dateutil>=2.5.0->pandas==0.24.2) (1.14.0)
Installing collected packages: pandas
  Found existing installation: pandas 1.0.0
    Uninstalling pandas-1.0.0:
      Successfully uninstalled pandas-1.0.0
    Running setup.py install for pandas ... /
然后等待了很长时间
Mac的CPU也狂转
等待时间太长,算了,中断
(2020.1.13已解决)ImportError: cannot import name StringIO_StringIO,pandas,tushare_漫步量化-CSDN博客
试试
pip install tushare --upgrade
结果还没来得及,终于又编译好了:
    Running setup.py install for pandas ... done
Successfully installed pandas-0.24.2
算了,暂时就用0.24.2的pandas
顺带升级一下pip
  pip install --upgrade pip
Collecting pip
  Downloading https://files.pythonhosted.org/packages/54/0c/d01aa759fdc501a58f431eb594a17495f15b88da142ce14b5845662c13f3/pip-20.0.2-py2.py3-none-any.whl (1.4MB)
     |███▏                            | 143kB 9.9kB/s eta 0:02:12^C
ERROR: Operation cancelled by user
WARNING: You are using pip version 19.3.1; however, version 20.0.2 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

 pip --proxy http://127.0.0.1:1087 install --upgrade pip
Collecting pip
  Downloading https://files.pythonhosted.org/packages/54/0c/d01aa759fdc501a58f431eb594a17495f15b88da142ce14b5845662c13f3/pip-20.0.2-py2.py3-none-any.whl (1.4MB)
     |████████████████████████████████| 1.4MB 96kB/s
Installing collected packages: pip
  Found existing installation: pip 19.3.1
    Uninstalling pip-19.3.1:
      Successfully uninstalled pip-19.3.1
Successfully installed pip-20.0.2
即可消除此处错误。
【总结】
此处Mac中Python:
import tushare
报错:
    from pandas.compat import StringIO
ImportError: cannot import name 'StringIO' from 'pandas.compat'
原因:
tushare依赖于pandas
最新版本1.0.0的pandas中已移除了pandas.compat
解决办法:
把pandas降低版本到0.24.2
pip install --upgrade pandas==0.24.2
即可。

转载请注明:在路上 » 【已解决】Mac中Python 3.8导入tushare报错:ImportError: cannot import name StringIO from pandas.compat

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
89 queries in 0.196 seconds, using 22.09MB memory