Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is absolute import in python?

Tags:

I am new to Python. I am developing a small project. I need to follow coding standards from starting on wards. How to use import statements in a proper way. Now I am working on Python 2.7. If I move to 3.x are there any conflicts with absolute imports? And what is the difference between absolute and relative imports?

like image 580
Shiva Avatar asked Mar 27 '14 05:03

Shiva


People also ask

What is an absolute import Python?

Absolute import involves a full path i.e., from the project's root folder to the desired module. An absolute import state that the resource is to be imported using its full path from the project's root folder.

Should I use relative or absolute imports Python?

With your new skills, you can confidently import packages and modules from the Python standard library, third party packages, and your own local packages. Remember that you should generally opt for absolute imports over relative ones, unless the path is complex and would make the statement too long.

What are the 3 types of import statements in Python?

Good practice is to sort import modules in three groups - standard library imports, related third-party imports, and local imports.

What are imports in Python?

In Python, you use the import keyword to make code in one module available in another. Imports in Python are important for structuring your code effectively. Using imports properly will make you more productive, allowing you to reuse code while keeping your projects maintainable.


1 Answers

The distinction between absolute and relative that's being drawn here is very similar to the way we talk about absolute and relative file paths or even URLs.

An absolute {import, path, URL} tells you exactly how to get the thing you are after, usually by specifying every part:

import os, sys from datetime import datetime from my_package.module import some_function 

Relative {imports, paths, URLs} are exactly what they say they are: they're relative to their current location. That is, if the directory structure changes or the file moves, these may break (because they no longer mean the same thing).

from .module_in_same_dir import some_function from ..module_in_parent_dir import other_function 

Hence, absolute imports are preferred for code that will be shared.


I was asked in comments to provide an example of how from __future__ import absolute_import ties into this, and how it is meant to be used. In trying to formulate this example, I realized I couldn't quite explain its behavior either, so I asked a new question. This answer gives a code sample showing a correctly working implementation of from __future__ import absolute_import, where it actually resolves an ambiguity.

The accepted answer goes into more detail about why this works the way it does, including a discussion of the confusing wording of the Python 2.5 changelog. Essentially, the scope of this directive (and by extension the distinction between absolute and relative imports in Python) is very, very narrow. If you find yourself needing these distinctions to make your code work, you're probably better off renaming your local module if at all possible.

like image 65
Two-Bit Alchemist Avatar answered Sep 28 '22 08:09

Two-Bit Alchemist