Trouble importing yahoo finance to python
Asked Answered
B

2

6

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)
Boyla answered 20/5, 2015 at 18:1 Comment(1)
Do you have multiple versions of Python installed? Perhaps pip is associated with the wrong one.Machismo
G
4

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.

Gubernatorial answered 20/5, 2015 at 18:26 Comment(3)
Thanks Will that was the issue!Boyla
Awesome, no problem :)Gubernatorial
Thanks, this worked for me: export PYTHONPATH="${PYTHONPATH}:/usr/local/lib/python2.7/dist-packages"Sedative
U
0

as a Windows user I couldn't quite follow Will's answer. Fortunetaly I came across a PYTHONPATH manager directly from my IDE (Spyder 5) and added the path in which the moduled was installed.

Altough the problem was solved, I still can't get mny head around the issue. The installation process was the same as for every other package I ever used, but in this case the path wasn't recognized, a common directory with the rest of my installed modules.

Unsound answered 2/2, 2022 at 15:18 Comment(1)
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.Alvaroalveolar

© 2022 - 2024 — McMap. All rights reserved.