【问题标题】:Python2 like behaviour in Python3Python3 中的类似 Python2 的行为
【发布时间】:2018-03-15 06:29:27
【问题描述】:

我正在运行 Jupyter Notebook,在 Macbook (OS X Sierra) 上运行 Python3。我最近所做的唯一更改是安装 Jupyter Notebook Extensions。现在我得到了类似于 Python 2.x 的除法,而不是 Python 3.x 的预期行为,它应该返回一个浮点数。

当我在终端中输入相同的命令时,这似乎不是问题。

什么解释了这种行为,我可以做些什么来恢复原来的 Python 3.x 部门?

【问题讨论】:

  • 看起来你确实有一个 Python 2 笔记本(尽管图片右上角有 Python 3)。 import sys; print(sys.version) 向您展示了什么?

标签: python python-3.x floating-point jupyter-notebook


【解决方案1】:

我会尝试 DOC 中的一些选项: http://python-future.org/compatible_idioms.html

您可以导入

from __future__ import division

所以 Python 2 和 3 处于“兼容模式”。您需要安装:

pip install future

希望这会有所帮助!

【讨论】:

  • 谢谢 - 但我已经好几个月没有使用 Python 2 了。我只有在需要运行任何旧代码时才拥有它。在 Jupyter 中,它已经在运行 Python 3 内核。
  • 你能打印你的 Jupyter Notebook Extension 版本吗?还有你的 IPython 版本?版本之间似乎有些差异:github.com/ipython/ipython
猜你喜欢
  • 2015-07-13
  • 1970-01-01
  • 2013-11-02
  • 2017-07-07
  • 1970-01-01
  • 2018-03-09
  • 2022-08-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多