0

why do I get error on the first module? for example in the following code no matter what I import first, I just get importError for the first module when I run it on CMD, for example if I put "requests" module at the top I get importError: No module named requests, and it happens for others if I put them as the first module. I pass it to CMD I get importError for the first module.

FYI: I installed all modules via pip install, and they worked well till yesterday. I haven't changed anything on my system up until now!

code:

#! python3
# lucky.py - opens several google search results
import bs4, sys, webbrowser, requests

print('Googling...') # display it while downloading the google page
res = requests.get('https://google.com/search?q=' + ' '.join(sys.argv[1:]))
res.raise_for_status()

# Retrieve top search result links.
soup = bs4.BeautifulSoup(res.text, features="html.parser")

# Open a browser tab for each result.
linkElems = soup.select('.r a')
numOpen = min(5, len(linkElems))
for i in range(numOpen):
    webbrowser.open('https://google.com' + linkElems[i].get('href'))

and this is the command and the error that I get on CMD:

C:\Windows\system32>lucky.py newyork
Traceback (most recent call last):
  File "D:\MyPythonScripts\lucky.py", line 3, in <module>
    import bs4, sys, webbrowser, requests
ImportError: No module named bs4

Thanks.

sf31
  • 45
  • 3
  • 10
  • Did you use a virtual environment when installing the moduled? – Klaus D. Oct 25 '19 at 06:14
  • check with the environment variables of python home and pip are added in the machine. – Parthasarathy Oct 25 '19 at 06:18
  • @KlausD. If you mean installing modules and running python on a virtual machine, no I didn't. But probably I installed the module while virtualbox was running. I can't remember exactly, but it's possible. so how can I fix it? – sf31 Oct 25 '19 at 06:44
  • @Parthasarathy What do you mean? how should I do that? – sf31 Oct 25 '19 at 06:48
  • Install with https://www.anaconda.com/distribution/. If you're new, you're better off using the Anaconda distribution. Use 64bit 3.7 with whatever your OS is. 2.7 is end of life in 2 months, so don't go there. – Trenton McKinney Oct 25 '19 at 06:50
  • I was not talking about virtual machines but about a virtual environment (aka. venv) a feature of Python to isolate library installations. – Klaus D. Oct 25 '19 at 06:54
  • @TrentonMcKinney I use python 3.7, and I download it from python official website.Tthe IDE is intellij. – sf31 Oct 25 '19 at 06:55
  • @KlausD. I haven't installed virtual env! I just used pip install ... – sf31 Oct 25 '19 at 06:59
  • @sf31 after installing the python you have to set the environment variables for python. So check that you have setup the python environment variable are not. If not check the [link](https://stackoverflow.com/questions/3701646/how-to-add-to-the-pythonpath-in-windows-so-it-finds-my-modules-packages) Also when executing the file do it as like python3 filename args – Parthasarathy Oct 25 '19 at 07:03
  • @Parthasarathy I already did it. and as I already said everything was fine till yesterday. I ran another code through CMD successfully. But today not this code nor others work!! – sf31 Oct 25 '19 at 07:37

1 Answers1

0

I solved it myself. The error is because of having two different versions of python on the system.(python 2.7 and python 3.7) The error disappeared after I deleted python 2. So there is nothing wrong with modules and pip install...

sf31
  • 45
  • 3
  • 10