简体   繁体   English

python3 中没有名为“beautifulsoup4”的模块

[英]No module named 'beautifulsoup4' in python3

$ virtualenv test
$ source test/bin/activate
$ pip3 install beautifulsoup4

Now the script test.py现在脚本 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()

when I run $python3 test.py当我运行$python3 test.py

File "test.py", line 6, in from bs4 import BeautifulSoup ModuleNotFoundError: No module named 'bs4'文件“test.py”,第 6 行,来自 bs4 import BeautifulSoup ModuleNotFoundError: No module named 'bs4'

then tried with然后尝试

from beautifulsoup4 import BeautifulSoup

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

requirements clearly shows both要求清楚地表明两者

$ 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

Only install BeautifulSoup4 instead of bs4 and BeautifulSoup then,然后只安装BeautifulSoup4而不是bs4BeautifulSoup

do this:做这个:

from bs4 import BeautifulSoup

Try to replace from beautifulsoup4 import BeautifulSoup in尝试替换 from beautifulsoup4 import BeautifulSoup in

from bs4 import BeautifulSoup

It works with me.它和我一起工作。

I would do this for a work around:我会这样做是为了解决:

All of this under your created virtual environment所有这些都在您创建的虚拟环境下

$ 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

It looks like you're not in the right place;看起来你来错地方了; cd test and try again. cd 测试并重试。

Here's the problem: Python uses the PYTHONPATH environment variable to look for packages, and by default that includes your current directory.问题在于:Python 使用 PYTHONPATH 环境变量来查找包,默认情况下它包括您的当前目录。 However, the BeautifulSoup4 package is installed under the test directory.但是,BeautifulSoup4 包安装在 test 目录下。 If you start Python from the test directory, it should be able to find it.如果test 目录启动 Python,它应该可以找到它。

Found the issue, virtualenv was installed with python2.7,发现问题,virtualenv是用python2.7安装的,

pip3 install virtualenv

fixed the issue.解决了这个问题。

What I did was to delete thebeautifulsoup4-4.8.0.dist-info and bs4 folders from C:\\Users\\User\\Anaconda3\\Lib\\site-packages and again performed pip install beautifulsoup4 on command prompt.我所做的是从C:\\Users\\User\\Anaconda3\\Lib\\site-packages删除bs4 thebeautifulsoup4-4.8.0.dist-infobs4文件夹,然后在命令提示符下再次执行pip install beautifulsoup4 It worked for me.它对我有用。

import bs4 worked for me. import bs4为我工作。 Both using an Anaconda env or Pip都使用 Anaconda env 或 Pip

from bs4 import BeautifulSoup

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM