Python __future__ 在特定模块之外
在 python 2.7 中,通过使用,
from __future__ import division, print_function
我现在可以让 print(1/2)
显示 0.5
。
但是,是否可以在 python 启动时自动导入它?
我尝试使用 sitecustomize.py
特殊模块,但输入仅在模块内部有效,而在 shell 中无效。
我确信人们会问我为什么需要这个:向青少年教授 Python,我注意到整数除法对他们来说并不容易,所以我们决定切换到 Python 3。然而,课程的要求之一是能够绘图函数和 Matplotlib 非常好,但仅对 Python 2.7 有效。
所以我的想法是使用自定义 2.7 安装...并不完美,但我没有更好的想法来同时拥有 Matplotlib 和新的“自然”除法“1/2=0.5”。
有什么建议或者可能是在 python 3.2 上工作的 Matplotlib 替代品吗?
In python 2.7, by using
from __future__ import division, print_function
I can now have print(1/2)
showing 0.5
.
However is it possible to have this automatically imported at python startup ?
I tried to use the sitecustomize.py
special module but the inport is only valid inside the module and not in the shell.
As I'm sure people will ask why I need that : teaching Python to teenagers I noticed that the integer division was not easy for them so we decided to switch to Python 3. However one of the requirement of the course was to be able to plot function and Matplotlib is pretty good but only valid for Python 2.7.
So my idea was to use a custom 2.7 installation...not perfect but I don't have a better idea to have both Matplotlib and the new "natural" division "1/2=0.5".
Any advice or maybe a Matplotlib alternative that is working on python 3.2 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
python 3 上的 matplotlib 比您想象的更接近: https://github.com/matplotlib/matplotlib-py3< /a>; http://www.lfd.uci.edu/~gohlke/pythonlibs/#matplotlib 。
为什么不使用 PYTHONSTARTUP 而不是 sitecustomize.py?
matplotlib on python 3 is closer than you may think: https://github.com/matplotlib/matplotlib-py3; http://www.lfd.uci.edu/~gohlke/pythonlibs/#matplotlib.
Why not use PYTHONSTARTUP instead of sitecustomize.py?
无需编译新版本的Python 2.x。您可以在启动时执行此操作。
正如您所发现的,sitecustomize.py 不起作用。这是因为
from __future__ import IDENTIFIER
不是 导入。它标记要在特殊规则下编译的模块。任何使用这些功能的模块都必须具有 __future__ 导入以及交互式控制台。以下 shell 命令将启动交互式控制台,并激活
division
和print_function
:您可以使用
python
别名(在 Linux 上)或设置启动器隐藏多余的东西。如果您使用的是 IDLE,@DSM 建议的
PYTHONSTARTUP
脚本也应该在那里工作。请注意,这些在整个解释器中并不是全局的,它只影响交互式控制台。文件系统上的模块必须从
__future__
显式导入才能使用该功能。如果这是一个问题,我建议制作一个模板来基于所有需要的导入进行工作:No need to compile a new version of Python 2.x. You can do this at start up.
As you found, sitecustomize.py does not work. This is because the
from __future__ import IDENTIFIER
isn't an import. It flags the module to be compiled under special rules. Any module that uses those features must have the__future__
import, as well as the interactive console.The following shell command will start the interactive console with
division
andprint_function
active:You could alias to
python
(on linux) or set up a launcher to hide the extra stuff.If you are using IDLE, the
PYTHONSTARTUP
script @DSM suggests should work there as well.Note that these are not global throughout the interpreter, it only affects the interactive console. Modules on the file-system must import from
__future__
explicitly to use the feature. If this is an issue, I suggest making a template to base work off of with all the needed imports:这可能不切实际,但您也许能够编译自定义 Python,并向后移植 Python 3 除法行为。问题是 matplotlib 可能需要 Python 2 行为(尽管我不确定)。
This may not be practical, but you may be able to compile a custom Python with the Python 3 division behavior backported. The problem with this is
matplotlib
might require the Python 2 behavior (although I'm not sure).