环境规划署:
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.0math.ceil返回一个浮点数,但是当我使用flask-script shell命令启动python时,它返回一个整数:
$ ./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.ceil的python3会返回一个整数,但是我用了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)]发布于 2018-01-08 09:24:29
我找到了根本原因,在我们的烧瓶应用程序中,我们添加了未来包。
未来是Python 2和Python 3之间缺少的兼容性层,它允许您使用一个干净的Python3.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-07 23:04:31
虽然它看起来像4是一个整数。这是有可能的烧瓶-脚本打印它没有十进制展开。试着跑
math.ceil(3.2)/3用烧瓶-剧本。如果它工作正常,它将打印1.3333333333333333。如果它打印1,那么您应该尝试重新安装烧瓶脚本。
https://stackoverflow.com/questions/48125239
复制相似问题