Python 3.x import error SyntaxError

笑着哭i 提交于 2019-12-12 17:07:42

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/40878863/python-3-x-import-error-syntaxerror

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!