Python 3.x import error SyntaxError

2019-07-20 16:28发布

I'm using macOS Sierra. When importing builtwith I get these following error:

Daniels-MacBook-Pro:~ Daniel$ python
Python 3.5.2 |Anaconda 4.2.0 (x86_64)| (default, Jul  2 2016, 17:52:12) 
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import builtwith
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/danielotero/anaconda3/lib/python3.5/site-packages/builtwith/__init__.py", line 43
    except Exception, e:
                    ^
SyntaxError: invalid syntax

What I can do to import it correctly?

2条回答
Ridiculous、
2楼-- · 2019-07-20 16:41

According to the module's issue tracker, it is not compatible with Python 3. The project owner says

This module was built with Python 2 in mind. Patches are welcome to also support Python 3, however would need to maintain backwards compatibility.

Since they don't appear to want to port it to Python 3 in order to remain backwards compatible, you should either use Python 2, look for another library, or attempt to port it yourself.

查看更多
看我几分像从前
3楼-- · 2019-07-20 16:57

This is because the builtwith package you installed is developed by Python2, not Python3. So it uses print and Exception as Python2 does. It also uses urllib2 library which is seperated into two part of urllib library in Python3.
It's better to use Python2 (Python2.7) to finish the work or you have to modify the source code of builtwith, that is, change all print statement into print() function, change except Exception, e into except Exception as e, and change all urllib2 functions into functions in urllib.requests and urllib.error.

查看更多
登录 后发表回答