已安装BeautifulSoup但仍未获得名为bs4的模块

时间:2018-01-29 00:02:46

标签: beautifulsoup virtualenv jupyter-notebook python-3.5

我正在使用Jupyter笔记本,Python 3.5和虚拟环境。

在我的虚拟环境中,我做了:

import numpy as np
from PyQt4.QtGui import *
from PyQt4.QtCore import *
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure


class Fig(FigureCanvas):
    def __init__(self, *args,**kwargs):
        self.factor = kwargs.pop("factor", 2)
        FigureCanvas.__init__(self, Figure(), *args,**kwargs)
        self.plot()
        self.scroll = QScrollBar(Qt.Horizontal)
        self.scroll.actionTriggered.connect(self.updateFromScroll)

    def plot(self):
        self.ax = self.figure.add_subplot(111)
        data = np.random.rand(1000)
        self.ax.plot(data, '-')
        self.xmin,self.xmax = self.ax.get_xlim()
        self.ax.set_xlim(self.xmin, self.xmax/float(self.factor))


    def updateFromScroll(self, evt):
        v = self.scroll.value()
        a = [self.xmin, self.xmax/float(self.factor)]
        lim = a+v/100.*np.diff(a)[0]
        self.ax.set_xlim(lim)
        self.draw_idle()


class Window(QMainWindow):
    def __init__(self):


        QMainWindow.__init__(self)
        self.widget = QWidget()
        self.setCentralWidget(self.widget)
        self.widget.setLayout(QVBoxLayout())
        self.widget.layout().setContentsMargins(0,0,0,0)
        self.widget.layout().setSpacing(0)

        self.canvas = Fig()
        self.canvas.draw()


        self.nav = NavigationToolbar(self.canvas, self.widget)
        self.widget.layout().addWidget(self.nav)
        self.widget.layout().addWidget(self.canvas)
        self.widget.layout().addWidget(self.canvas.scroll)

        self.show()


qapp = QApplication([])
# pass the figure to the custom window
a = Window()
exit(qapp.exec_()) 

终端输出似乎运行良好b / c:

(venv) > pip install BeautifulSoup4

但是当我运行我的Jupyter笔记本时,我得到了:

Downloading beautifulsoup4-4.6.0-py2-none-any.whl (86kB)
  100% |████████████████████████████████| 92kB 297kB/s 
Installing collected packages: BeautifulSoup4
Successfully installed BeautifulSoup4-4.6.0

我无法将ImportError Traceback (most recent call last) <ipython-input-1-5fe69888b1a1> in <module>() 5 from itertools import groupby 6 import pickle ----> 7 import bs4 8 import matplotlib.pyplot as plt 9 get_ipython().run_line_magic('matplotlib', 'inline') ImportError: No module named 'bs4' 行更改为import bs4我在其他SO帖子上看到b / c是只读的,我不应该

1 个答案:

答案 0 :(得分:4)

由于您使用的是Python 3,我认为您需要执行

pip3 install BeautifulSoup4

只需pip install就可以使用Python 2包了。

其他要检查的事项:

首先确保你在virtualenv中运行了BeautifulSoup安装,在virtualenv中运行了python3提示符或Jupyter笔记本。如果您在根环境中进行了安装并且笔记本电脑在virtualenv中运行,反之亦然,那么您的site-packages目录可能不匹配。

通过在命令提示符下键入pip3 list,并注意它出现在列表中,确保您的软件包确实已安装。

检查你的Jupyter笔记本确实正在运行Python 3.5内核(浏览器窗口的右上角)

检查软件包安装的路径是否在sys.path

打开Python3提示或在Jupyter中运行

import sys
print (sys.path)