Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: force non-relative import?

I wanted to make a module called utils/django.py in my project. On the top I have the line:

from django.db import models

However, it tries to import from itself, and that causes an error. I know I can force a relative import with a prepended .:

from .django.db import models

is there any way to force a non-relative import?

like image 572
Claudiu Avatar asked Mar 21 '11 00:03

Claudiu


People also ask

Should I use relative or absolute imports Python?

With your new skills, you can confidently import packages and modules from the Python standard library, third party packages, and your own local packages. Remember that you should generally opt for absolute imports over relative ones, unless the path is complex and would make the statement too long.

What is __ all __ in Python?

PACKAGES. In the __init__.py file of a package __all__ is a list of strings with the names of public modules or other objects. Those features are available to wildcard imports. As with modules, __all__ customizes the * when wildcard-importing from the package.

Why do I need __ init __ py?

The __init__.py files are required to make Python treat directories containing the file as packages. This prevents directories with a common name, such as string , unintentionally hiding valid modules that occur later on the module search path.


1 Answers

No. You need to explicitly enable absolute imports.

from __future__ import absolute_import
like image 108
Ignacio Vazquez-Abrams Avatar answered Sep 28 '22 08:09

Ignacio Vazquez-Abrams