What is absolute import in Python and how is it used?

0 votes

Hi all, I am fairly new to Python. Looking for some basic help.

How to use import statements in a proper way. Now I use Python 2.7. If I move to 3.x are there any conflicts with absolute imports? And also what is the difference between absolute and relative imports?

Nov 30, 2018 in Python by Anirudh
• 2,070 points

edited Dec 10, 2018 by Anirudh 101 views

1 answer to this question.

0 votes

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.

answered Nov 30, 2018 by Nymeria
• 3,520 points

edited Dec 10, 2018 by Nymeria

Related Questions In Python

0 votes
3 answers
+4 votes
7 answers
0 votes
3 answers

What is python? Where it is used?

 Python is develop by Guido Van Rossum ...READ MORE

answered Mar 18 in Python by rajesh kumar
65 views
+1 vote
3 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 328 views
+4 votes
6 answers
0 votes
1 answer

What is logits, softmax and softmax_cross_entropy_with_logits in Python?

Suppose you have two tensors, where y_hat contains computed ...READ MORE

answered Nov 12, 2018 in Python by Nymeria
• 3,520 points
749 views
0 votes
1 answer

Is multi-threading supported in Python and can it speed up execution time as well?

The GIL does not prevent threading. All ...READ MORE

answered Nov 22, 2018 in Python by Nymeria
• 3,520 points
187 views