首页 > 解决方案 > 有没有办法为我的脚本指定最低 python 版本要求,因为 pipfile 不支持这个?

问题描述

有没有办法为我的脚本指定最低 python 版本要求?例如,我的脚本需要 python 3.6+,因为它使用 f-string。我已经在 3.7、3.8、3.9 下测试了我的脚本,它们都可以工作。

但是由于 pipfile 不支持最低版本,请参考pipenv 在 pipfile 中指定 python 的最低版本?和这个未解决的问题https://github.com/pypa/pipfile/issues/87

那么有没有办法做到这一点?现在我只是把它写在自述文件中。

- - 更新 - -

https://github.com/pypa/pipenv/issues/2683确实说过

请注意,python_version 键是可选的。如果它引起问题,您总是可以删除它,否则记录版本要求(即在自述文件中)。

标签: pythonpython-3.xpyenv

解决方案


检查这篇文章。这可能会满足您的要求:

#!/usr/bin/env python
import sys

if sys.version_info[0] != 3 or sys.version_info[1] < 6:
    print("This script requires Python version 3.6")
    sys.exit(1)

# rest of script, including real initial imports, here

推荐阅读