Search code examples
pythonyahoo-finance

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')
print(BlackDiamond.get_open)

Solution

  • 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.