Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

from . import XXXX

Tags:

python

In one of my Python packages the __init__.py file contains the statement

from . import XXXX

What does the "." mean here? I got this technique by looking at another package, but I don't understand what it means.

Thanks!

like image 673
jlconlin Avatar asked Sep 14 '11 13:09

jlconlin


People also ask

What is import * in Python?

Python code in one module gains access to the code in another module by the process of importing it. The import statement is the most common way of invoking the import machinery, but it is not the only way. Functions such as importlib.

How do I use Python import?

You need to use the import keyword along with the desired module name. When interpreter comes across an import statement, it imports the module to your current program. You can use the functions inside a module by using a dot(.) operator along with the module name.

How do I import a Python path?

append() Function. This is the easiest way to import a Python module by adding the module path to the path variable. The path variable contains the directories Python interpreter looks in for finding modules that were imported in the source files.

What is from in Python?

The from keyword is used to import only a specified section from a module.


2 Answers

Its a relative import. From: http://docs.python.org/py3k/reference/simple_stmts.html#the-import-statement

When specifying what module to import you do not have to specify the absolute name of the module. When a module or package is contained within another package it is possible to make a relative import within the same top package without having to mention the package name. By using leading dots in the specified module or package after from you can specify how high to traverse up the current package hierarchy without specifying exact names.

One leading dot means the current package where the module making the import exists. Two dots means up one package level. Three dots is up two levels, etc. So if you execute from . import mod from a module in the pkg package then you will end up importing pkg.mod. If you execute from ..subpkg2 import mod from within pkg.subpkg1 you will import pkg.subpkg2.mod. The specification for relative imports is contained within PEP 328.

like image 123
utdemir Avatar answered Sep 21 '22 22:09

utdemir


It's a relative import.

like image 38
mouad Avatar answered Sep 20 '22 22:09

mouad