Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python __future__ outside of a specific module

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 ?

like image 219
Loïc Février Avatar asked Sep 05 '11 02:09

Loïc Février


People also ask

What is the __ future __ module in Python?

__future__ module is a built-in module in Python that is used to inherit new features that will be available in the new Python versions.. This module includes all the latest functions which were not present in the previous version in Python.

What does from __ future __ import do?

Future statements tell the interpreter to compile some semantics as the semantics which will be available in the future Python version. In other words, Python uses from __future__ import feature to backport features from other higher Python versions to the current interpreter.

What is __ all ))?

In the __init__.py file of a package __all__ is a list of strings with the names of public modules or other objects. Those features are available to wildcard imports. As with modules, __all__ customizes the * when wildcard-importing from the package.

What is __ future __ import Absolute_import?

from __future__ import absolute_import means that if you import string , Python will always look for a top-level string module, rather than current_package.string . However, it does not affect the logic Python uses to decide what file is the string module.


2 Answers

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,>>> 
like image 57
DSM Avatar answered Oct 02 '22 16:10

DSM


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()
like image 30
Erik Youngren Avatar answered Oct 02 '22 18:10

Erik Youngren