1

环境:

python: 2.7.5
flask = 0.12.2
flask-cors = 2.0.1
flask-script = 2.0.5

启动一个 python 外壳:

$ python
Python 2.7.5 (default, Nov  6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> math.ceil(3.2)
4.0

math.ceil返回一个浮点数,但是当我使用Shellflask-script命令启动 python shell 时,它返回一个整数:

$ ./bin/python manage.py shell
==========================================
Starting server at 2018-01-06 15:30:14
==========================================

In [1]: import math

In [2]: math.ceil(3.2)
Out[2]: 4

我知道math.ceilpython3会返回一个整数,但我用的是python2,有人有好主意吗?

检查python版本:

In [1]: import sys

In [2]: print (sys.version)
2.7.5 (default, Nov  6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
4

2 回答 2

0

我找到了根本原因,对于我们的烧瓶应用程序,我们添加了未来包。

future 是 Python 2 和 Python 3 之间缺少的兼容层。它允许您使用单一、干净的 Python 3.x 兼容代码库以最小的开销同时支持 Python 2 和 Python 3。

这个包重写了math.ceil方法,它返回一个整数:

In [5]: math.ceil.func_code
Out[5]: <code object ceil at 0x7fd74853cab0, file "/data/apps/ecoboost/eggs/future-0.16.0-py2.7.egg/future/backports/misc.py", line 31>

源代码如下:

def ceil(x):
    """
    Return the ceiling of x as an int.
    This is the smallest integral value >= x.
    """
    return int(oldceil(x))
于 2018-01-08T09:24:29.053 回答
0

虽然看起来 4 是一个整数。烧瓶脚本可能在没有小数扩展的情况下打印它。尝试运行

math.ceil(3.2)/3

在烧瓶脚本中。如果它工作正常,它将打印1.3333333333333333. 如果它打印1,那么你应该尝试重新安装flask-script。

于 2018-01-07T23:04:31.583 回答