Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do python modules work in shell but not in the script?

I am trying to make two programs. I want one to print the current weather of my city of residence and I want the other one to take data from an online account and return it. For those scripts I import the yweather module and the requests module. When I import them in the shell there are no problems but when I run the script it says "ImportError: No module named yweather". What am I doing wrong?

Shell:

>>> import requests
>>> 

Script:

Traceback (most recent call last):
  File "/Users/tim/Desktop/login.py", line 1, in <module>
    import requests
ImportError: No module named requests 

This also happens for the yweather module

Thank you

like image 834
Tim Angevare Avatar asked Oct 18 '22 10:10

Tim Angevare


1 Answers

I have a same problem as you, but with package 'sklearn'. With scikit-learn and sklearn installed, I run import sklearn in a .py file and it returns "ModuleNotFoundError: No module named 'sklearn.ensemble'; 'sklearn' is not a package".

It turns out I made a funny mistake. I named the file 'sklearn.py'. So when I import sklearn, it probably just trys to import itself. I shouldn't have named that file 'sklearn.py'.

like image 199
Tianyu Z Avatar answered Oct 21 '22 01:10

Tianyu Z