将雅虎财经导入python时遇到问题 [英] Trouble importing yahoo finance to python

查看:25
本文介绍了将雅虎财经导入python时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 pip 从 PyPI 安装了 yahoo Finance,当我运行以下脚本时,出现导入错误:没有名为 yahoo_finance 的模块

I have installed yahoo finance from the PyPI with pip, and when I go to run the following script i get an import error: No module named yahoo_finance

from yahoo_finance import Share

BlackDiamond = Share('BDE')
print(BlackDiamond.get_open)

推荐答案

确保 pip 安装到 Python 的包含路径中的某个位置.运行这个命令:

Make sure pip installed to somewhere in Python's include path. Run this command:

$ 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

看到它说位置:/usr/local/lib/python2.7/site-packages?确保您的是系统站点包目录.通常(例如,在 Mac 或 Ubuntu 上)您需要 sudo pip install 以将它们放入系统站点包中.如果您打算以用户身份将其安装到主目录中的某个位置,则需要确保该目录位于您的 python 路径中.

See where it says Location: /usr/local/lib/python2.7/site-packages? Make sure yours is the system site-packages directory. Often (for example, on Mac or Ubuntu) you need to sudo pip install to get them in system site-packages. If your intention is to install it as a user to somewhere in your home directory, you need to ensure that directory is in your python-path.

要查看您当前的路径设置,请在您的主目录中创建一个名为 path.py 的文件,并包含以下内容:

To see your current path settings, create a file called path.py in your home directory and include the following:

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,你应该会看到类似这样的输出:

Run python path.py and you should see output similar to this:

$ 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 的路径在您的路径配置中.如果不是,您可以通过 .bashrc 和/或 .bash_profile 修改 $PYTHONPATH:

Now, make sure the path where yahoo_finance was installed is inside your path configuration. If it's not, you can modify $PYTHONPATH via your .bashrc and/or .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.

Then, you should be able to include your module. Again, though: If you're installing a system-wide site package, you probably just want to use sudo pip.

这篇关于将雅虎财经导入python时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆