Open In App

How to Fix 'No Module Named yfinance' Error in Python

Last Updated : 26 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

If you encounter the 'No Module Named yfinance' error, it typically means that the library is not installed or not accessible in your current Python environment. This issue can be resolved by ensuring that yfinance is properly installed and that your Python environment is configured correctly.

What is Python Code Error: No Module Named ‘yfinance’?

The error “No Module Named ‘yfinance'” indicates that the Python interpreter cannot find the ‘yfinance’ module, possibly because it is not installed. To resolve this, you can install the ‘yfinance’ module using a package manager like PIP by running the command: pip3 install yfinance. Ensure that your Python environment and dependencies are correctly configured.

Screenshot-2024-07-22-110441

Why does Python Code Error: No Module Named ‘yfinance’ occur?

Below, are the reasons for “Modulenotfounderror: No Module Named ‘yfinance′” In Python occurring:

  • Module Not Installed
  • Incorrect Module Name

Module Not Installed

In this scenario, we are trying to import the yfinance module in the code snippet where the data is to be fetched from the Endpoint. But, as the module yfinance is not installed on the system, the Python Code Error: No Module Named ‘yfinance’ will occur.

Python
import yfinance
ticker = "MSFT"
microsoft = yfinance.Ticker(ticker)
hist = microsoft.history(period="1mo")
print(hist['Close'])

Output:

Traceback (most recent call last):
File ".\script.py", line 1, in <module>
import yfinance
ModuleNotFoundError: No module named 'yfinance'

Incorrect Module Name

Another reason for the error might be a typo or incorrect naming when trying to import the yfinance module. Python is case-sensitive, so ensure that the module name is spelled correctly.

Python
import YFINANCE # Incorrect

Output:

Traceback (most recent call last):
File ".\script.py", line 1, in <module>
import YFINANCE
ModuleNotFoundError: No module named 'YFINANCE'

Solve “Modulenotfounderror: No Module Named ‘yfinance′”

Below, are the approaches to solve “Modulenotfounderror: No Module Named ‘yfinance′”.

  • Install yfinance Module
  • Check Module Name

Install yfinance Module

We can resolve this error by installing the yfinance package externally using the PIP package manager. Execute the below command in the prompt to install the package.

pip3 install yfinance

Once we execute the command, it will take a couple of minutes to completely install the package according to the internet speed.

Screenshot-2024-07-22-110836-min

Check Module Name

Please double-check the spelling and case sensitivity of the module name when importing it into your script.

Python
import yfinance # Correct

Conclusion

In conclusion, error commonly occurs in situations where you are trying to run a Python script or application that relies on the ‘yfinance’ module, but the module is not installed in your Python environment. Ensure you have the required modules installed using the appropriate package manager (pip in this case) before executing the script.


Next Article
Article Tags :
Practice Tags :

Similar Reads