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

PythonImport

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

Possible duplicate: https://stackoverflow.com/questions/7279810/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.

Solution 2 - Python

In addition of Sudeep Juvekar, this question is also related to manage.py's behavior.

In django-admin.py and manage.py:
> It puts your project’s package on sys.path.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionwobbily_colView Question on Stackoverflow
Solution 1 - PythonSudeep JuvekarView Answer on Stackoverflow
Solution 2 - PythonZuluView Answer on Stackoverflow