#!/usr/bin/python import sys try: import yum except ImportError: print >> sys.stderr, """\ There was a problem importing one of the Python modules required to run yum. The error leading to this problem was: %s Please install a package which provides this module, or verify that the module is installed correctly. It's possible that the above module doesn't match the current version of Python, which is: %s If you cannot solve this problem yourself, please go to the yum faq at: http://yum.baseurl.org/wiki/Faq """ % (sys.exc_value, sys.version) sys.exit(1) sys.path.insert(0, '/usr/share/yum-cli') try: import yummain yummain.user_main(sys.argv[1:], exit_code=True) except KeyboardInterrupt, e: print >> sys.stderr, "\n\nExiting on user cancel." sys.exit(1) 使用python3语法如何修改
时间: 2024-04-04 14:31:56 浏览: 66
你可以将 `print` 函数的语法从 Python 2 转换为 Python 3 的语法,还需要修改 `except` 捕获异常的语法。修改后的代码如下:
```python
#!/usr/bin/python3
import sys
try:
import yum
except ImportError:
print("""There was a problem importing one of the Python modules required to run yum. The error leading to this problem was:
{}
Please install a package which provides this module, or verify that the module is installed correctly. It's possible that the above module doesn't match the current version of Python, which is: {}
If you cannot solve this problem yourself, please go to the yum faq at:
http://yum.baseurl.org/wiki/Faq""".format(sys.exc_info()[1], sys.version), file=sys.stderr)
sys.exit(1)
sys.path.insert(0, '/usr/share/yum-cli')
try:
import yummain
yummain.user_main(sys.argv[1:], exit_code=True)
except KeyboardInterrupt as e:
print("\n\nExiting on user cancel.", file=sys.stderr)
sys.exit(1)
```
这样就可以使用 Python 3 来执行这个脚本了。
阅读全文