Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make absolute_import the default in all modules

After reading on the web, I noticed that there was a promise made that Python 2.7 would use absolute imports as the default. However it seems it is not the case, and that we still have to use

from __future__ import absolute_import

I don't want my code to use some legacy settings, so I want to make sure all the modules have this enabled. How to do that, without having to repeat this statement in every single module?

like image 714
Flavien Avatar asked Nov 03 '22 23:11

Flavien


1 Answers

from __future__ imports must be done first, and are module specific. There is no easy way to have it in effect for all your modules without actually having the line be in all your modules.

I believe you can use grep (if not, roll your own) to tell you which of your modules do not have that line in them.

If you want to do it the hard way, take a look at either importlib, or replacing __import__. Using either of these two methods you could open the .py file, insert the

from __future__ import absolute_import

line (if not already there), and then do the actual import. Take care that you don't add that line to other modules besides your own, however, as you will undoubtedly get errors from modules expecting the relative import semantics.

like image 186
Ethan Furman Avatar answered Nov 15 '22 04:11

Ethan Furman