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?
Ans. One can import the same package or same class multiple times.
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.
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.
So each module is imported only one time.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With