Trouble importing yahoo finance to python

28,785

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: [email protected]
License: MIT
Location: /usr/local/lib/python2.7/site-packages
Requires: pytz, simplejson
Entry-points:
  [console_scripts]
  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 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

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

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 path.py
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.

Share:
28,785
Tony Victor
Author by

Tony Victor

Updated on July 05, 2022

Comments

  • Tony Victor
    Tony Victor almost 2 years

    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)
    
  • Tony Victor
    Tony Victor about 9 years
    Thanks Will that was the issue!
  • Will
    Will about 9 years
    Awesome, no problem :)
  • pbnelson
    pbnelson over 8 years
    Thanks, this worked for me: export PYTHONPATH="${PYTHONPATH}:/usr/local/lib/python2.7/dist-pack‌​ages"
  • Admin
    Admin over 2 years
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.