导入错误:没有名为 bs4 的模块(BeautifulSoup)

问题描述:

我正在使用 Python 并使用 Flask.当我在我的计算机上运行我的主要 Python 文件时,它工作得很好,但是当我激活 venv 并在终端中运行 Flask Python 文件时,它说我的主要 Python 文件有没有名为 bs4 的模块".非常感谢任何意见或建议.

I'm working in Python and using Flask. When I run my main Python file on my computer, it works perfectly, but when I activate venv and run the Flask Python file in the terminal, it says that my main Python file has "No Module Named bs4." Any comments or advice is greatly appreciated.

激活virtualenv,然后安装BeautifulSoup4:

Activate the virtualenv, and then install BeautifulSoup4:

$ pip install BeautifulSoup4

当您使用 easy_install 安装 bs4 时,您在系统范围内安装了它.所以你的系统 python 可以导入它,但不能导入你的 virtualenv python.如果您不需要 bs4 安装在您的系统 python 路径中,请将其卸载并保留在您的 virtualenv 中.

When you installed bs4 with easy_install, you installed it system-wide. So your system python can import it, but not your virtualenv python. If you do not need bs4 to be installed in your system python path, uninstall it and keep it in your virtualenv.

有关 virtualenvs 的更多信息,请阅读this

For more information about virtualenvs, read this