首页 > 解决方案 > ImportError:在终端中运行时没有名为 bs4 的模块

问题描述

我正在尝试通过终端运行 python 脚本,但不断收到以下错误:

'''
   Traceback (most recent call last):
      File "note9.py", line 1, in <module>
        from bs4 import BeautifulSoup
    ImportError: No module named bs4
'''

我已经尝试卸载 bs4 并重新安装 bs4。

奇怪的是我能够在 Visual Studio 代码中运行脚本。

'''
User:PythonProjects username$ /Library/Frameworks/Python.framework/Versions/3.8/bin/python3 /Users/username/Desktop/PythonProjects/note9.py
Unavailable
'''

为什么我在尝试通过终端运行脚本时收到错误消息?

from bs4 import BeautifulSoup
from requests import get
import yagmail 

def lovely_soup(u):
    r = get(u, headers={
            'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1'})
    return BeautifulSoup(r.text, 'lxml')


url = 'https://www.baka.ca/Samsung/Samsung-Galaxy-Note-9-128GB-LTE-Blue'
soup = lovely_soup(url)
for h2 in soup.find_all('h2'):
    if 'Unavailable' in h2.text:
        yagmail.SMTP('mygmailusername').send('to@someone.com', 'subject', contents)    
        print('Unavailable')
    else:
        yagmail.SMTP('mygmailusername').send('to@someone.com', 'subject', contents)
        print('Available')

'''

以下是我在终端运行时收到的错误

Tylers-MacBook-Air-2:PythonProjects tylerarie$ python note9.py
Traceback (most recent call last):
  File "note9.py", line 1, in <module>
    from bs4 import BeautifulSoup
ImportError: No module named bs4
Tylers-MacBook-Air-2:PythonProjects tylerarie$ pip3 install bs4
Requirement already satisfied: bs4 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (0.0.1)
Requirement already satisfied: beautifulsoup4 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from bs4) (4.8.1)
Requirement already satisfied: soupsieve>=1.2 in /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages (from beautifulsoup4->bs4) (1.9.5)
Tylers-MacBook-Air-2:PythonProjects tylerarie$ 

它确实说该模块已安装?为什么我不能在终端运行这个脚本!!!???

该脚本在 VSC 中运行良好。

The default interactive shell is now zsh.
To update your account to use zsh, please run `chsh -s /bin/zsh`.
For more details, please visit https://support.apple.com/kb/HT208050.
Tylers-MacBook-Air-2:PythonProjects tylerarie$ /Library/Frameworks/Python.framework/Versions/3.8/bin/python3 /Users/tylerarie/Desktop/PythonProjects/note9.py
Unavailable
Tylers-MacBook-Air-2:PythonProjects tylerarie$ 

标签: visual-studiobeautifulsoupterminalimporterror

解决方案


推荐阅读