lenakmeth
lenakmeth

Reputation: 91

BeautifulSoup invalid syntax in Python 3.4 (after 2to3.py)

I am trying to install Beautiful Soup 4 in Python 3.4. I installed it from the command line, (got the invalid syntax error because I had not converted it), ran the 2to3.py conversion script to bs4 and now I get a new invalid syntax error.

>>> 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'

Any ideas?

Upvotes: 3

Views: 10922

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121176

BeautifulSoup 4 does not need manual converting to run on Python 3. You are trying to run code only compatible with Python 2 instead; it appears you failed to correctly convert the codebase.

From the BeautifulSoup 4 homepage:

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

The line now throwing the exception should read:

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

The codebase does use Python 2 syntax, but the setup.py installer converts this for you to compatible Python 3 syntax. Make sure to install the project with pip:

pip install beautifulsoup4

or using the pip version bundled with Python 3.4:

python3.4 -m pip install beautifulsoup4

or using easy_install:

easy_install beautifulsoup4

If you downloaded just the tarball, at the very least run

python3.4 setup.py install

to have the installer correctly convert the codebase for you; the converted code is copied into your Python setup. You can discard the downloaded source directory after running the command, see How installation works.

Alternatively, run:

python3.4 setup.py build

and copy across the build/lib directory. Again, do not use the original source directory as it is left untouched.

Upvotes: 8

Related Questions