在第一个导入的模块上获取importError

问题描述 投票:0回答:1

为什么在第一个模块上出现错误?例如,在下面的代码中,无论我先导入什么,在CMD上运行该模块时,我只会得到第一个模块的importError,例如,如果将“ requests”模块放在顶部, importError:没有名为请求的模块,如果我将其他模块作为第一个模块,则会发生其他情况。我将其传递给CMD,第一个模块得到importError。

FYI:我通过pip install安装了所有模块,它们运行良好,直到昨天。到目前为止,我还没有更改系统上的任何内容!

代码:

#! 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'))

这是命令和我在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

谢谢。

python search beautifulsoup python-requests
1个回答
0
投票

我自己解决了。该错误是由于系统上有两个不同版本的python(python 2.7和python 3.7)删除python 2后,错误消失了。因此,模块和pip安装没有错...

© www.soinside.com 2019 - 2024. All rights reserved.