Beautiful Soup 无法在命令行界面中运行

DCh*_*aps 0 python pip beautifulsoup command-line-interface python-3.x

我试图让漂亮的汤在 python 3 的 cli 中运行,这样我就可以尝试并找出如何最好地使用它。我是通过pip安装的。

pip3 list
Run Code Online (Sandbox Code Playgroud)

节目

Package        Version
-------------- -------
beautifulsoup4 4.8.0  
Run Code Online (Sandbox Code Playgroud)

然而,当我运行 python3 CLI 并尝试导入 beautiful soup 来玩时,我收到错误:

>>> import beautifulsoup4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'beautifulsoup4'
Run Code Online (Sandbox Code Playgroud)

我还尝试通过 CLI 导入 bs4,但这很奇怪:

>>> import bs4
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/derekchapman/Google Drive/Code/Python/p4e/bs4/__init__.py", line 48
    'You are trying to run the Python 2 version of Beautiful Soup under Python 3. This will not work.'<>'You need to convert the code, either by installing it (`python setup.py install`) or by running 2to3 (`2to3 -w bs4`).'
Run Code Online (Sandbox Code Playgroud)

我安装并运行了 python3,如果我将 bs4 导入到我的代码中,它运行良好,没有错误。

Fly*_*ler 5

错误消息中的路径/Users/derekchapman/Google Drive/Code/Python/表明您已添加了一个自定义文件夹PYTHONPATH。在此文件夹中,您安装了一个python2版本beautifulsoup,当您安装时import,您的python解释器首先找到该模块,因为环境变量PYTHONPATH不是特定于 python 版本的。

您现在有多种选择:

  1. 从您的 中删除该文件夹PYTHONPATH,但这可能会破坏python2您可能首先添加的一些代码
  2. 从该文件夹中删除beautifulsoup4,但这与第一种方法有相同的警告
  3. 如果您想坚持使用多个 python 版本,那么最稳定也是我的建议:清除PYTHONPATH并删除自定义安装的 python 版本,然后使用虚拟环境来管理不同的 python 版本和模块安装