Python __future__ outside of a specific module

In python 2.7 using

 from __future__ import division, print_function

Now I have a print(1/2)showing 0.5.

However, is it possible to import automatically when python starts up?

I tried to use a special module sitecustomize.py, but inport is valid only inside the module, and not in the shell.

As I'm sure, people will ask why I need this: teaching Python to teens, I noticed that integer separation was not easy for them, so we decided to switch to Python 3. However, one of the requirements of the course was to be able to build a function and Matplotlib is pretty good, but only applicable for Python 2.7.

So, my idea was to use a custom installation of 2.7 ... not perfect, but I have no better idea of ​​having both Matplotlib and the new "natural" split of "1/2 = 0.5".

Any advice or alternative to Matplotlib working on python 3.2?

+5
source share
3 answers

matplotlib on python 3 is closer than you think: https://github.com/matplotlib/matplotlib-py3 ; http://www.lfd.uci.edu/~gohlke/pythonlibs/#matplotlib .

Why not use PYTHONSTARTUP instead of the sitecustomize.py file?

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,>>> 
+6
source

No need to compile a new version of Python 2.x. You can do this at startup.

, sitecustomize.py . , from __future__ import IDENTIFIER . . , , __future__, .

division print_function:

python -ic "from __future__ import division, print_function"

python ( linux) , .

IDLE, PYTHONSTARTUP script @DSM.

, , . __future__, . , :

# True division
from __future__ import division

# Modules
import matplotlib

# ... code ...

def main():
    pass

if __name__ == "__main__":
    main()
+2

This may not be practical, but you can compile custom Python with Python 3 reverse-link behavior. A problem with this matplotlibmay require Python 2 behavior (although I'm not sure).

0
source

All Articles