美化Python 3.4中的无效语法(在2to3.py之后)

2024-04-26 13:58:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在Python3.4中安装漂亮的Soup4。我从命令行安装了它(因为我没有转换它,所以得到了无效语法错误),运行了2to3.py转换脚本到bs4,现在得到了一个新的无效语法错误。

>>> from bs4 import BeautifulSoup
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
    from bs4 import BeautifulSoup
  File "C:\Python34\bs4\__init__.py", line 30, in <module>
    from .builder import builder_registry, ParserRejectedMarkup
  File "C:\Python34\bs4\builder\__init__.py", line 4, in <module>
    from bs4.element import (
  File "C:\Python34\bs4\element.py", line 1213
    print 'Running CSS selector "%s"' % selector
                                    ^
SyntaxError: Missing parentheses in call to 'print'

有什么想法吗?


Tags: infrompyimportinitbuilderlineelement
1条回答
网友
1楼 · 发布于 2024-04-26 13:58:21

BeautifulSoup 4需要手动转换才能在Python 3上运行。您试图运行的代码只与Python2兼容;似乎无法正确转换代码库。

BeautifulSoup 4 homepage

Beautiful Soup 4 works on both Python 2 (2.6+) and Python 3.

现在引发异常的行应该是:

print('Running CSS selector "%s"' % selector)

代码库确实使用了Python2语法,但是setup.py安装程序将其转换为兼容的Python3语法。请确保使用pip安装项目:

pip install beautifulsoup4

或者使用与Python 3.4捆绑在一起的pip版本:

python3.4 -m pip install beautifulsoup4

或者使用easy_install

easy_install beautifulsoup4

如果你只下载了tarball,至少可以运行

python3.4 setup.py install

为了让安装程序正确地为您转换代码库,将转换后的代码复制到Python设置中。运行命令后可以放弃下载的源目录,请参见How installation works

或者,运行:

python3.4 setup.py build

并跨build/lib目录复制。同样,不要使用原始源目录,因为它没有被修改。

相关问题 更多 >