Tim Angevare
Tim Angevare

Reputation: 101

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

Upvotes: 8

Views: 6788

Answers (5)

salabara
salabara

Reputation: 1

I just encountered a similar situation as yours. The module being imported is opensbli. Here is the install guide: https://github.com/opensbli/opensbli/blob/version2.0/docs/installation_guide.pdf

The mistake I made is that I first cloned it to /opt/ by sudo command, and move to the directory owned by my user. The problem is that since I clone it with sudo at first, the owner of the opensbli directory is root instead of my user, and this seems to be the reason I can't import it in script even with sudo command.

The fix is simple. Just remove the clone and clone it again without sudo.

Upvotes: 0

Tianyu Z
Tianyu Z

Reputation: 151

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

Upvotes: 8

Nico Coallier
Nico Coallier

Reputation: 686

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

Upvotes: 0

thedarkgriffen
thedarkgriffen

Reputation: 414

Maybe you are using a virtual environment while in script, and modules are not installed there.

Upvotes: 0

codeiscool
codeiscool

Reputation: 119

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.

Upvotes: 0

Related Questions