Skip to content Skip to sidebar Skip to footer

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 conversi

Solution 1:

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.

Post a Comment for "Beautifulsoup Invalid Syntax In Python 3.4 (after 2to3.py)"