问题
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
回答1:
Are you sure it is the same version of Python? Try running the following in both the shell and in a script, compare the results.
import sys
sys.version
If they are not identical, you have two versions installed.
回答2:
Maybe you are using a virtual environment while in script, and modules are not installed there.
回答3:
If you are on Windows , you probably have install Python twice . Did you install Python with Anaconda and install Python independently?
When you type Python in the command prompt do you get the same Python version that the one in your interpreter ?
If yes then go in your system panel and delete the program for the Python version running on command prompt .
Otherwise you can check what pythonpath you are using in the command prompt :
echo %PATH%
If different one way of fixing it in the Python interpreter :
import sys
sys.path.append('your certain directory')
Or you can also set a new path in the command prompt with :
setx PATH "%PYTHONPATH%;C:\python27"
Hope this helps
来源:https://stackoverflow.com/questions/44588533/why-do-python-modules-work-in-shell-but-not-in-the-script