8

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

7
  • 1
    Are you sure you're using the same interpreter in both cases? Commented Jun 16, 2017 at 11:51
  • Are you sure that you use the same Python version while running script and in the prompt? Commented Jun 16, 2017 at 11:51
  • Show us the first line of the script. Commented Jun 16, 2017 at 11:51
  • @sphere The first line is exactly import requests as we can see in the error Commented Jun 16, 2017 at 11:52
  • 2
    OK, then no shebang is in the script itself like #!/bin/python. Then show us how exactly did you run the script in the shell. Commented Jun 16, 2017 at 11:54

5 Answers 5

8

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

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

They both return the same. But after trying several things I found out that I can run the scripts without a problem when using terminal.
0

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

Comments

0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.