Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python "from [dot]package import ..." syntax [duplicate]

Tags:

python

import

Looking through a Django tutorial I saw the following syntax:

from .models import Recipe, Ingredient, Instruction 

Can someone explain how the .models works / what it does exactly? Usually I have:

from myapp.models import 

How does it work without the myapp part in front of .models?

like image 950
wobbily_col Avatar asked Mar 19 '14 16:03

wobbily_col


People also ask

Can we import same package twice in Python?

Ans. One can import the same package or same class multiple times.

What is from DOT import in Python?

Relative imports make use of dot notation to specify location. A single dot means that the module or package referenced is in the same directory as the current location. Two dots mean that it is in the parent directory of the current location—that is, the directory above.

What does it mean to import * Python?

Importing refers to allowing a Python file or a Python module to access the script from another Python file or module. You can only use functions and properties your program can access. For instance, if you want to use mathematical functionalities, you must import the math package first.

Does Python import module multiple times?

So each module is imported only one time.


1 Answers

Possible duplicate: What does a . in an import statement in Python mean?

The . is a shortcut that tells it to search in the current package before the rest of the PYTHONPATH. So, if a same-named module Recipe exists somewhere else in your PYTHONPATH, it won't be loaded.

like image 76
Sudeep Juvekar Avatar answered Oct 01 '22 21:10

Sudeep Juvekar