这个问题告诉我如何检查Python的版本。对于我的包,我至少需要 Python 3.3:
MIN_VERSION_INFO = 3, 3
import sys
if not sys.version_info >= MIN_VERSION_INFO:
exit("Python {}.{}+ is required.".format(*MIN_VERSION_INFO))
Run Code Online (Sandbox Code Playgroud)
但是这个检查应该在哪里/什么时候发生?
我想为通过pip(sdist 和 wheel)或python setup.py install. 就像是:
$ pip -V
pip x.x.x from ... (Python 3.2)
$ pip install MyPackage
Python 3.3+ is required.
$ python -V
Python 3.2
$ python setup.py install
Python 3.3+ is required.
Run Code Online (Sandbox Code Playgroud) 我有几个软件包可以安装不同的 Python 安装。例如:
C:\Python27\ArcGIS10.1
C:\Python27\ArcGIS10.2
C:\Python27|ArcGISx6410.1
Run Code Online (Sandbox Code Playgroud)
使用sys.version不适用于我的情况,因为我需要知道实际安装的位置,而不是版本。
如何确定我的 Python 解释器正在使用哪个安装?
类似于__author__或__version__顶层模块的变量,有指定支持Python版本的Python源文件中的任何约定?
我的用例是一个项目,其中包含一些需要与Python 2.4兼容的脚本.我想用一些普遍可识别的方式注意它们中的事实.
我不是在询问如何在执行期间要求最小的Python版本.我的问题是开发人员可能会意外地使用与此特定脚本需要支持的python版本不兼容的功能.PyCharm可以警告Python不兼容性.如果能够获取此注释并在每个文件的基础上配置警告,那将会很棒.
它看起来像python完美的工作......
$ python
Python 2.7.8 (default, Aug 24 2014, 21:26:19)
[GCC 4.2.1 Compatible Apple LLVM 5.1 (clang-503.0.40)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
Run Code Online (Sandbox Code Playgroud)
但python -v打印出各种垃圾:
$ python -v
# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
# /usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.pyc matches /usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.py
import site # precompiled from /usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site.pyc
# /usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc matches /usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.py
import os # precompiled from /u
<..................>
/usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_sysconfigdata.pyc matches /usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_sysconfigdata.py
import _sysconfigdata # precompiled from /usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_sysconfigdata.pyc
# /usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_osx_support.pyc matches /usr/local/Cellar/python/2.7.8_1/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_osx_support.py …Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的程序顶部编写一个代码块,如果程序意外地在Python 2.x中运行,它将给出一条错误消息并退出,但如果在Python中运行3.x将正常运行:
try:
print "Error: This program should only be run in Python 3."
raw_input('>')
exit()
except SyntaxError:
pass
print("I see you're running Python 3.")
# rest of program
Run Code Online (Sandbox Code Playgroud)
这在Python 2.7中正常工作(即,它显示错误和退出),但是当我在Python 3.3中运行它时,我得到一个SyntaxError,即使我告诉它有一个例外.
File "version.py", line 2
print "This program should only be run in Python 3"
^
SyntaxError: invalid syntax
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
正如问题所要求的,我想确保脚本是由特定版本的python执行的,例如=> 3.5.2。
如何确保执行时的脚本由特定版本调用。
此检查应在python脚本本身中完成。
如果解决方案是平台无关的,那就更好了。
python ×7
python-3.x ×2
homebrew ×1
interpreter ×1
linux ×1
macos ×1
pip ×1
pycharm ×1
python-2.7 ×1
syntax-error ×1
version ×1
windows ×1