Python3.x导入错误语法

2024-06-11 22:50:14 发布

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

我用的是macOS Sierra。导入builtwith时,出现以下错误:

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

如何正确导入?在


Tags: default错误linemacosanacondax86julsierra
2条回答

这是因为您安装的builtwith包是由Python2开发的,而不是Python3。所以它像Python2一样使用print和{}。它还使用urllib2库,该库在Python3中分为两部分。
最好使用Python2(Python2.7)来完成这项工作,否则必须修改builtwith的源代码,即将所有print语句改为print()函数,将except Exception, e改为except Exception as e,并将所有urllib2函数改为urllib.requests和{}中的函数。在

根据module's issue tracker,它与python3不兼容。项目业主说

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

因为他们似乎不想将它移植到python3以保持向后兼容,所以您应该使用python2,寻找另一个库,或者尝试自己移植它。在

相关问题 更多 >