【问题标题】:No module named 'beautifulsoup4' in python3python3中没有名为'beautifulsoup4'的模块
【发布时间】:2018-08-16 15:19:50
【问题描述】:
$ virtualenv test
$ source test/bin/activate
$ pip3 install beautifulsoup4

现在脚本 test.py

import urllib.request
import sys
import unittest, time, re

import requests
from bs4 import BeautifulSoup

class Sel(unittest.TestCase):

    def setUp(self):
        self.base_url = "file:///Users/ishandutta2007/Desktop/cars/cars-price-list.html"

    def test_sel(self):
        with urllib.request.urlopen(self.base_url) as url:
            html_source = url.read()
        data = html_source#.encode('utf-8')
        parsed_html = BeautifulSoup(data, 'html.parser')

if __name__ == "__main__":
    unittest.main()

当我运行$python3 test.py

文件“test.py”,第 6 行,在 from bs4 import BeautifulSoup ModuleNotFoundError: No module named 'bs4'

然后尝试使用

from beautifulsoup4 import BeautifulSoup

文件“test.py”,第 6 行,在 from beautifulsoup4 import BeautifulSoup ModuleNotFoundError: No module named 'beautifulsoup4'

需求清楚地表明了两者

$ pip3 freeze > requirements.txt
$ cat requirements.txt 
beautifulsoup4==4.6.0
certifi==2018.1.18
chardet==3.0.4
idna==2.6
requests==2.18.4
urllib3==1.22

【问题讨论】:

标签: python python-3.x beautifulsoup


【解决方案1】:

然后只安装BeautifulSoup4 而不是bs4 和BeautifulSoup,

这样做:

from bs4 import BeautifulSoup

【讨论】:

  • 正确阅读问题
  • 我读得很正确,我说只安装beautifulsoup4 而不是bs4。换句话说,删除bs4 并只安装beautifulsoup4 正确阅读答案:)
  • 在你的问题开始你pip3 install beautifulsoup4 然后你也做pip3 install bs4 我是说只做pip install beautifulsoup4
  • 假设我还没有完成pip3 install bs4,现在回答。
  • 然后更改文件名可能python正在执行其他一些test.py
【解决方案2】:

尝试替换 from beautifulsoup4 import BeautifulSoup 在

from bs4 import BeautifulSoup

它对我有用。

【讨论】:

    【解决方案3】:

    我会这样做以解决问题:

    所有这些都在你创建的虚拟环境下

    $ wget https://pypi.python.org/pypi/beautifulsoup4
    $ pip3 install beautifulsoup4-4.6.0-py3-none-any.whl
    $ pip3 freeze |grep beautifulsoup4
    $ python3
    >>> from bs4 import BeautifulSoup
    

    【讨论】:

      【解决方案4】:

      看来您来错地方了; cd 测试并重试。

      问题在于:Python 使用 PYTHONPATH 环境变量来查找包,并且默认情况下包括您的当前目录。但是,BeautifulSoup4 包安装在 test 目录下。如果你从 test 目录启动 Python,它应该能够找到它。

      【讨论】:

        【解决方案5】:

        发现问题,virtualenv是用python2.7安装的,

        pip3 install virtualenv
        

        修复了问题。

        【讨论】:

          【解决方案6】:

          我所做的是从C:\Users\User\Anaconda3\Lib\site-packages 中删除thebeautifulsoup4-4.8.0.dist-info 和bs4 文件夹,然后在命令提示符下再次执行pip install beautifulsoup4。 它对我有用。

          【讨论】:

            【解决方案7】:

            import bs4 为我工作。两者都使用 Anaconda env 或 Pip

            from bs4 import BeautifulSoup
            

            【讨论】:

            • 这在其他答案中已经提到了。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2020-10-23
            • 2021-09-27
            • 1970-01-01
            • 2021-11-09
            • 1970-01-01
            • 2011-05-18
            • 2018-01-20
            相关资源
            最近更新 更多