我已经使用pip从PyPI安装了yahoo finance,当我运行以下脚本时,我收到导入错误:没有名为yahoo_finance的模块
from yahoo_finance import Share
BlackDiamond = Share('BDE')
print(BlackDiamond.get_open)
答案 0 :(得分:2)
确保将pip
安装到Python的include路径中的某个位置。运行此命令:
$ pip show yahoo-finance
---
Metadata-Version: 1.1
Name: yahoo-finance
Version: 1.2.1
Summary: Python module to get stock data from Yahoo! Finance
Home-page: https://github.com/lukaszbanasiak/yahoo-finance
Author: Lukasz Banasiak
Author-email: lukasz@banasiak.me
License: MIT
Location: /usr/local/lib/python2.7/site-packages
Requires: pytz, simplejson
Entry-points:
[console_scripts]
yahoo-finance = yahoo_finance:main
查看Location: /usr/local/lib/python2.7/site-packages
的位置?确保您的系统是site-packages目录。通常(例如,在Mac或Ubuntu上),您需要sudo pip install
才能在系统站点包中获取它们。如果您打算以用户身份将其安装到主目录中的某个位置,则需要确保该目录位于python路径中。
要查看当前路径设置,请在主目录中创建名为path.py
的文件,并包含以下内容:
import os
import sys
try:
user_paths = os.environ['PYTHONPATH'].split(os.pathsep)
except KeyError:
user_paths = []
print "PYTHONPATH: ", user_paths
print "sys.path: ", sys.path
运行python path.py
,你会看到与此类似的输出:
$ python path.py
PYTHONPATH: ['/usr/local/lib/python2.7/site-packages', '']
sys.path: ['/Users/me/dir', '/usr/local/Cellar/python/2.7.9/..../lib/python2.7/lib-dynload', '/Library/Python/2.7/site-packages', '/usr/local/lib/python2.7/site-packages']
现在,确保安装yahoo_finance
的路径位于路径配置中。如果不是,您可以通过$PYTHONPATH
和/或.bashrc
修改.bash_profile
:
export PYTHONPATH="${PYTHONPATH}:/path/to/your/dir"
例如:
$ export PYTHONPATH="${PYTHONPATH}:/path/to/your/dir"
$ python path.py
PYTHONPATH: ['/usr/local/lib/python2.7/site-packages', '', '/path/to/your/dir']
然后,您应该能够包含您的模块。但是,如果您要安装系统范围的站点包,则可能只想使用sudo pip
。