Python __future__ 在特定模块之外

发布于 2024-12-03 02:14:46 字数 607 浏览 0 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遮了一弯 2024-12-10 02:14:46

python 3 上的 matplotlib 比您想象的更接近: https://github.com/matplotlib/matplotlib-py3< /a>; http://www.lfd.uci.edu/~gohlke/pythonlibs/#matplotlib

为什么不使用 PYTHONSTARTUP 而不是 sitecustomize.py?

localhost-2:~ $ cat startup.py 
from __future__ import print_function
from __future__ import division
localhost-2:~ $ export PYTHONSTARTUP=""
localhost-2:~ $ python
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 1/2
0
>>> print("fred",end=",")
  File "<stdin>", line 1
    print("fred",end=",")
                    ^
SyntaxError: invalid syntax
>>> ^D
localhost-2:~ $ export PYTHONSTARTUP=startup.py
localhost-2:~ $ python
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 1/2
0.5
>>> print("fred",end=",")
fred,>>> 

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?

localhost-2:~ $ cat startup.py 
from __future__ import print_function
from __future__ import division
localhost-2:~ $ export PYTHONSTARTUP=""
localhost-2:~ $ python
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 1/2
0
>>> print("fred",end=",")
  File "<stdin>", line 1
    print("fred",end=",")
                    ^
SyntaxError: invalid syntax
>>> ^D
localhost-2:~ $ export PYTHONSTARTUP=startup.py
localhost-2:~ $ python
Python 2.7.2 (v2.7.2:8527427914a2, Jun 11 2011, 15:22:34) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 1/2
0.5
>>> print("fred",end=",")
fred,>>> 
凹づ凸ル 2024-12-10 02:14:46

无需编译新版本的Python 2.x。您可以在启动时执行此操作。

正如您所发现的,sitecustomize.py 不起作用。这是因为 from __future__ import IDENTIFIER 不是 导入。它标记要在特殊规则下编译的模块。任何使用这些功能的模块都必须具有 __future__ 导入以及交互式控制台。

以下 shell 命令将启动交互式控制台,并激活 divisionprint_function

python -ic "from __future__ import division, print_function"

您可以使用 python 别名(在 Linux 上)或设置启动器隐藏多余的东西。

如果您使用的是 IDLE,@DSM 建议的 PYTHONSTARTUP 脚本也应该在那里工作。

请注意,这些在整个解释器中并不是全局的,它只影响交互式控制台。文件系统上的模块必须__future__显式导入才能使用该功能。如果这是一个问题,我建议制作一个模板来基于所有需要的导入进行工作:

# True division
from __future__ import division

# Modules
import matplotlib

# ... code ...

def main():
    pass

if __name__ == "__main__":
    main()

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 and print_function active:

python -ic "from __future__ import division, print_function"

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:

# True division
from __future__ import division

# Modules
import matplotlib

# ... code ...

def main():
    pass

if __name__ == "__main__":
    main()
放低过去 2024-12-10 02:14:46

这可能不切实际,但您也许能够编译自定义 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文