简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM