Tony Victor Tony Victor - 11 months ago 71
Python Question

Trouble importing yahoo finance to python

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')


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
Author: Lukasz Banasiak
License: MIT
Location: /usr/local/lib/python2.7/site-packages
Requires: pytz, simplejson
  yahoo-finance = yahoo_finance:main

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.

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

import os
import sys

    user_paths = os.environ['PYTHONPATH'].split(os.pathsep)
except KeyError:
    user_paths = []

print "PYTHONPATH: ", user_paths
print "sys.path: ", sys.path

Run python and you should see output similar to this:

$ python
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']

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"

For example:

$ export PYTHONPATH="${PYTHONPATH}:/path/to/your/dir"
$ python
PYTHONPATH:  ['/usr/local/lib/python2.7/site-packages', '', '/path/to/your/dir']

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.