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,050 points

edited Dec 10, 2018 by Anirudh 52 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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,500 points

edited Dec 10, 2018 by Nymeria

Related Questions In Python

0 votes
2 answers

What is a Tuple in Python and how to use it?

  Tuple is a collection of Python objects.it is ...READ MORE

answered Apr 12 in Python by rajesh
• 1,210 points
40 views
+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
40 views
0 votes
2 answers

What do you mean by python scripting? What is a script and a module in python?

A scripting language is a programming language ...READ MORE

answered Mar 14 in Python by rajesh kumar
1,480 views
0 votes
1 answer

how can i count the items in a list?

suppose you have a list a = [0,1,2,3,4,5,6,7,8,9,10] now ...READ MORE

answered May 2 in Python by Mohammad
• 920 points
18 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,500 points
260 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,500 points
63 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.