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

【已解决】mac中pip安装Python库tushare

Python crifan 1912浏览 0评论
折腾:
【提醒】Python新手开发人员注意事项:测试文件名不要和导入的库同名
期间,去mac中用pip安装tushare库:
 limao@xxx  ~  which python
/Users/limao/.pyenv/shims/python
 limao@xxx  ~  python --version
Python 3.8.0
 limao@xxx  ~  which pip
/Users/limao/.pyenv/shims/pip
 limao@xxx  ~  pip --version
pip 19.3.1 from /Users/limao/.pyenv/versions/3.8.0/Python.framework/Versions/3.8/lib/python3.8/site-packages/pip (python 3.8)
 limao@xxx  ~  pip install tushare
Collecting tushare
  Downloading 
https://files.pythonhosted.org/packages/bf/3f/149ef2a80cddd24410e6788228a22f3b3afb5c55c765a223a5c7ecf93ecf/tushare-1.2.48-py3-none-any.whl (211kB)
     |████████████████████████████████| 215kB 11kB/s
Installing collected packages: tushare
Successfully installed tushare-1.2.48
然后终端中试试Python的shell中导入是否正常
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 as ts
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 13, in <module>
    import lxml.html
ModuleNotFoundError: No module named 'lxml'
>>>
[1]  + 86707 suspended  python
发现缺少lxml,所以去安装
 ✘ ⚙ limao@xxx  ~  pip install lxml
Collecting lxml
  WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out. (read timeout=15)")': /packages/86/e5/3412ef8d7870c4075dcd832298e07f2bfa2c18879aeea5d931f7096f27ba/lxml-4.5.0-cp38-cp38-macosx_10_9_x86_64.whl
  Downloading https://files.pythonhosted.org/packages/86/e5/3412ef8d7870c4075dcd832298e07f2bfa2c18879aeea5d931f7096f27ba/lxml-4.5.0-cp38-cp38-macosx_10_9_x86_64.whl (4.6MB)
     |████████████████████████████████| 4.6MB 11kB/s
Installing collected packages: lxml
Successfully installed lxml-4.5.0
再去运行,发现又少pandas
 ⚙ 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 15, in <module>
    import pandas as pd
ModuleNotFoundError: No module named 'pandas'
>>>
KeyboardInterrupt
>>>
[2]  + 87046 suspended  python
再去安装,发现速度太慢,中断
 ✘ ⚙ limao@xxx  ~  pip install pandas
Collecting pandas
  Downloading 
https://files.pythonhosted.org/packages/50/a6/7265e2557887430e39d89523577b848ff4586cd12caf986262ee810091aa/pandas-1.0.0-cp38-cp38-macosx_10_9_x86_64.whl (9.9MB)
     |███▏                            | 993kB 15kB/s eta 0:09:52^C
ERROR: Operation cancelled by user
参考自己之前的
【已解决】Mac中给pip3添加代理以提升下载python包的速度 – 在路上
加上代理
 ✘ ⚙ limao@xxx  ~  pip --proxy http://127.0.0.1:1087 install pandas
Collecting pandas
  Downloading https://files.pythonhosted.org/packages/50/a6/7265e2557887430e39d89523577b848ff4586cd12caf986262ee810091aa/pandas-1.0.0-cp38-cp38-macosx_10_9_x86_64.whl (9.9MB)
     |████████████████████████████████| 9.9MB 162kB/s
Collecting pytz>=2017.2
  Using cached https://files.pythonhosted.org/packages/e7/f9/f0b53f88060247251bf481fa6ea62cd0d25bf1b11a87888e53ce5b7c8ad2/pytz-2019.3-py2.py3-none-any.whl
Collecting numpy>=1.13.3
  Downloading https://files.pythonhosted.org/packages/a7/06/6d616fb5fb423db595b1502cbd873f3f2025f2fd8509046c771a20c4302a/numpy-1.18.1-cp38-cp38-macosx_10_9_x86_64.whl (15.2MB)
     |████████████████████████████████| 15.2MB 114kB/s
Collecting python-dateutil>=2.6.1
  Using cached https://files.pythonhosted.org/packages/d4/70/d60450c3dd48ef87586924207ae8907090de0b306af2bce5d134d78615cb/python_dateutil-2.8.1-py2.py3-none-any.whl
Collecting six>=1.5
  Downloading https://files.pythonhosted.org/packages/65/eb/1f97cb97bfc2390a276969c6fae16075da282f5058082d4cb10c6c5c1dba/six-1.14.0-py2.py3-none-any.whl
Installing collected packages: pytz, numpy, six, python-dateutil, pandas
Successfully installed numpy-1.18.1 pandas-1.0.0 python-dateutil-2.8.1 pytz-2019.3 six-1.14.0
结果去导入,又报错:
【已解决】Mac中Python 3.8导入tushare报错:ImportError: cannot import name StringIO from pandas.compat
然后又缺少requests
 ⚙ 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 82, in <module>
    from tushare.pro.data_pro import (pro_api, pro_bar)
  File "/Users/limao/.pyenv/versions/3.8.0/Python.framework/Versions/3.8/lib/python3.8/site-packages/tushare/pro/data_pro.py", line 11, in <module>
    from tushare.pro import client
  File "/Users/limao/.pyenv/versions/3.8.0/Python.framework/Versions/3.8/lib/python3.8/site-packages/tushare/pro/client.py", line 14, in <module>
    import requests
ModuleNotFoundError: No module named 'requests'
>>>
[5]  + 88139 suspended  python
去安装:
 ~  pip install requests
Collecting requests
  Using cached requests-2.22.0-py2.py3-none-any.whl (57 kB)
Collecting certifi>=2017.4.17
  Using cached certifi-2019.11.28-py2.py3-none-any.whl (156 kB)
Collecting urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1
  Downloading urllib3-1.25.8-py2.py3-none-any.whl (125 kB)
     |████████████████████████████████| 125 kB 6.3 kB/s
Collecting chardet<3.1.0,>=3.0.2
  Using cached chardet-3.0.4-py2.py3-none-any.whl (133 kB)
Collecting idna<2.9,>=2.5
  Using cached idna-2.8-py2.py3-none-any.whl (58 kB)
Installing collected packages: certifi, urllib3, chardet, idna, requests
Successfully installed certifi-2019.11.28 chardet-3.0.4 idna-2.8 requests-2.22.0 urllib3-1.25.8
然后:
  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 129, in <module>
    from tushare.futures.domestic import (get_cffex_daily, get_czce_daily,
  File "/Users/limao/.pyenv/versions/3.8.0/Python.framework/Versions/3.8/lib/python3.8/site-packages/tushare/futures/domestic.py", line 11, in <module>
    from bs4 import BeautifulSoup
ModuleNotFoundError: No module named 'bs4'
再去安装bs4
 pip install bs4
Collecting bs4
  Downloading bs4-0.0.1.tar.gz (1.1 kB)
Collecting beautifulsoup4
  Downloading beautifulsoup4-4.8.2-py3-none-any.whl (106 kB)
     |████████████████████████████████| 106 kB 14 kB/s
Collecting soupsieve>=1.2
  Downloading soupsieve-1.9.5-py2.py3-none-any.whl (33 kB)
Installing collected packages: soupsieve, beautifulsoup4, bs4
    Running setup.py install for bs4 ... done
Successfully installed beautifulsoup4-4.8.2 bs4-0.0.1 soupsieve-1.9.5
最后终于正常导入tushare了:
~  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
>>> print(tushare.__version__)
1.2.48
>>>
【总结】
此处Mac中,安装tushare,需要先安装依赖的库:
pip install lxml pandas==0.24.2 requests bs4
再去安装tushare
pip install tushare
才可以。
然后测试是否安装成功
~  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
>>> print(tushare.__version__)
1.2.48
>>>
确认是正常安装和可以正常使用了。

转载请注明:在路上 » 【已解决】mac中pip安装Python库tushare

发表我的评论
取消评论

表情

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

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