【问题标题】:What is the pythonic way of specifying a minimal module version?指定最小模块版本的pythonic方法是什么?
【发布时间】:2016-09-19 16:06:25
【问题描述】:

我正在编写一个 Python 脚本,其中一个导入的模块需要至少为 x.y.z 版本。

import pandas as pd

# Check the pandas version
_pdversion = map(lambda x: int(x), pd.__version__.split('.'))
if _pdversion[1] < 18:
    # Not sure the most sensible exception class..
    raise Exception("ERROR: you need pandas > version 0.18")

是否有在运行时检查最小版本的标准方法?还是不鼓励这样做,而支持在包级别和配置时进行规范?

【问题讨论】:

  • setup.py 要求可能是最好的方法

标签: python dependencies packaging


【解决方案1】:

我认为最 Pythonic 的方式是在 requirements.txt 文件中指定它,并将所有内容保存在虚拟环境中。

或者:

import pkg_resources
pkg_resources.require("requests==2.11.1")
import requests

【讨论】:

    猜你喜欢
    • 2011-09-16
    • 2015-09-11
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    • 2010-10-04
    • 1970-01-01
    • 2014-05-22
    • 2010-11-23
    相关资源
    最近更新 更多