Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use absolute imports, even intra-package #2

Closed
wants to merge 1 commit into from
Closed

use absolute imports, even intra-package #2

wants to merge 1 commit into from

Conversation

JonnyJD
Copy link

@JonnyJD JonnyJD commented Mar 28, 2012

Starting with Python 3, imports are always absolute by default.
This means a package is searched in the library path
and in the current directory of the active script.

Intra-package imports can bo done in two ways:
from packagename import module (absolute)
from . import module (relative)

With absolute imports you have to use the actual name of the package and
change the import when the name of the package changes.
The relative import is "name-agnostic".

However, there are also downsides with the relative approach:

Relative imports break outside of the package context.
python -m pkg/module breaks with:
ValueError: Attempted relative import in non-package
in Python 2 and 3.
Making a quick syntax-check impossible and breaking
epydoc with:
Bad dotted name

Therefore we use absolute imports unless there is a specific
reason to use relative imports.

Please note that you can still use a library in the same folder (locally)
without installing it to a library path.

Starting with Python 3, imports are always absolute by default.
This means a package is searched in the library path
and in the current directory of the active script.

Intra-package imports can bo done in two ways:
from packagename import module  (absolute)
from . import module		(relative)

With absolute imports you have to use the actual name of the package and
change the import when the name of the package changes.
The relative import is "name-agnostic".

However, there are also downsides with the relative approach:

Relative imports break outside of the package context.
```python -m pkg/module``` breaks with:
```ValueError: Attempted relative import in non-package```
in Python 2 and 3.
Making a quick syntax-check impossible and breaking
epydoc with:
```Bad dotted name```

Therefore we use absolute imports unless there is a specific
reason to use relative imports.

Please note that you can still use a library in the same folder (locally)
without installing it to a library path.

Signed-off-by: Johannes Dewender <[email protected]>
@mineo
Copy link
Owner

mineo commented Mar 28, 2012

If you use python{2,} -m pkg.module everything works as expected.

@JonnyJD
Copy link
Author

JonnyJD commented Mar 28, 2012

Well not "everything". You can use pkg.module for easy syntax checking, but epydoc still doesn't work.

There might or might not be a fix for epydoc. I will see what I can do in my maintenance fork epydoc-m. The official version is not maintained anymore.

Apart from that, is there a strong reason to use relative imports?

@JonnyJD
Copy link
Author

JonnyJD commented Mar 28, 2012

A patch for epydoc exists, it just wasn't included in the abandoned official version: JonnyJD/epydoc-m#6

So now "everything" works as expected for me ;-)

@mineo mineo closed this Jun 14, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants