Skip to main content
 首页 » 编程设计

python 的 gi 模块与 python3 不兼容

2025年02月15日8虾米哥

我正在尝试运行也使用“gi”模块的 python(版本 3.6.2)程序。 当我尝试导入“gi”时,出现以下错误:

Traceback (most recent call last): 
  File "<stdin>", line 1, in <module> 
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/gi/__init__.py", line 39 
    print url 
            ^ 
SyntaxError: Missing parentheses in call to 'print' 

我唯一要做的就是导入它。

import gi 

请您参考如下方法:

您可以使用工具 2to3 是一个 Automated Python 2 to 3 code translation ,你可以抛开旧的 python 并使更改在 python 3 中工作,我做了一个简单的例子,如下所示:

我创建了这个文件,python2.py:

#!python3 
 
print 5 

当我用 python 运行它时,它显然显示:

line 3 
    print 5          ^ 
SyntaxError: Missing parentheses in call to 'print' 

因此,您可以像这样通过终端转换它:

This is the important comand

$ 2to3 -w home/path_to_file/python2.py 

-w 参数将写入文件,如果您只想查看 future 的更改而不应用它们,只需在没有 -w 的情况下运行它。 运行后会显示类似

root: Generating grammar tables from /usr/lib/python2.7/lib2to3/PatternGrammar.txt 
RefactoringTool: Refactored Desktop/stackoverflow/goto.py 
--- Desktop/stackoverflow/goto.py   (original) 
+++ Desktop/stackoverflow/goto.py   (refactored) 
@@ -1,3 +1,3 @@ 
 #!python3 
 
-print 5 
+print(5) 
RefactoringTool: Files that were modified: 

文件看起来像:

#!python3 
 
print(5)