是否检查编写的代码是否在python 2.7或3及更高版本中? [英] Any check to see if the code written is in python 2.7 or 3 and above?

查看:99
本文介绍了是否检查编写的代码是否在python 2.7或3及更高版本中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个bug较长的python项目,我正在尝试调试.它杂乱无章.我熟悉python2.7.该项目中没有二进制文件.直截了当的想法是尝试以python2.7 file.pypython3 file.py的身份执行它,然后看看哪个可行.但是正如我所说,在很多地方它已经是越野车了.因此,它们都不起作用.是否有任何检查,方法或编辑器可以告诉我代码是用python2.7还是python3编写的?

I have a buggy long python project that I am trying to debug. Its messy and undocumented. I am familiar with python2.7. There are no binaries in this project. The straight forward idea is to try execute it as python2.7 file.py or python3 file.py and see which works. But as I said it is already buggy at a lot of places. So none of them is working. Is there any check or method or editor that could tell me if the code was written in python2.7 or python3?

推荐答案

尝试对其进行编译.如果脚本使用特定于版本的语法,则编译将失败.

Attempt to compile it. If the script uses syntax specific to a version then the compilation will fail.

$ python2 -m py_compile foo.py
$ python3 -m py_compile foo.py

这篇关于是否检查编写的代码是否在python 2.7或3及更高版本中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆