ℹ️ Skipped - page is already crawled
| Filter | Status | Condition | Details |
|---|---|---|---|
| HTTP status | PASS | download_http_code = 200 | HTTP 200 |
| Age cutoff | PASS | download_stamp > now() - 6 MONTH | 0.2 months ago |
| History drop | PASS | isNull(history_drop_reason) | No drop reason |
| Spam/ban | PASS | fh_dont_index != 1 AND ml_spam_score = 0 | ml_spam_score=0 |
| Canonical | PASS | meta_canonical IS NULL OR = '' OR = src_unparsed | Not set |
| Property | Value |
|---|---|
| URL | https://philipmolloy.com/import.html |
| Last Crawled | 2026-04-05 09:35:23 (5 days ago) |
| First Indexed | 2019-04-09 15:14:10 (7 years ago) |
| HTTP Status Code | 200 |
| Meta Title | Absolute imports in __main__ |
| Meta Description | null |
| Meta Canonical | null |
| Boilerpipe Text | I began to brush the dust off an old Python project by running the
2to3
, a Python program that can automate code translation
from Python 2 to 3. The first suggestion was to replace the import
statement in a __main__ file as follows:
-from reference.wiktionary import Wiktionary
+from .reference.wiktionary import Wiktionary
This change was based on a fixer that “detects sibling imports and
converts them to relative imports” (see
Python
documentation
). An explanation for this fixer can be found in the
Intra-package
References
article, which states, “Note that relative imports are
based on the name of the current module. Since the name of the main
module is always”__main__“, modules intended for use as the main module
of a Python application must always use absolute imports.”
Absolute imports are explained by
PEP
328
, which states, “In Python 2.4 and earlier, if you’re reading a
module located inside a package, it is not clear whether import foo
refers to a top-level module or to another module inside the package. …
foo will always be a module or package reachable from sys.path. This is
called an absolute import.”
See also
Nightmare with
relative imports, how does pep 366 work?
Using
modules in main.py |
| Markdown | [Contents](https://philipmolloy.com/)
May 14, 2013
# Absolute imports in \_\_main\_\_
I began to brush the dust off an old Python project by running the `2to3`, a Python program that can automate code translation from Python 2 to 3. The first suggestion was to replace the import statement in a \_\_main\_\_ file as follows:
```
-from reference.wiktionary import Wiktionary
+from .reference.wiktionary import Wiktionary
```
This change was based on a fixer that “detects sibling imports and converts them to relative imports” (see [Python documentation](https://docs.python.org/2/library/2to3.html#2to3fixer-import)). An explanation for this fixer can be found in the [Intra-package References](http://docs.pytho.org/3.3/tutorial/modules.html#intra-package-references) article, which states, “Note that relative imports are based on the name of the current module. Since the name of the main module is always”\_\_main\_\_“, modules intended for use as the main module of a Python application must always use absolute imports.”
Absolute imports are explained by [PEP 328](https://www.python.org/dev/peps/pep-0328/#rationale-for-absolute-imports), which states, “In Python 2.4 and earlier, if you’re reading a module located inside a package, it is not clear whether import foo refers to a top-level module or to another module inside the package. … foo will always be a module or package reachable from sys.path. This is called an absolute import.”
## See also
- [Nightmare with relative imports, how does pep 366 work?](http://stackoverflow.com/questions/2943847)
- [Using modules in main.py](http://stackoverflow.com/questions/3411293/using-modules-own-objects-in-main-py)
© 2011-2025 Philip Molloy
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. |
| Readable Markdown | I began to brush the dust off an old Python project by running the `2to3`, a Python program that can automate code translation from Python 2 to 3. The first suggestion was to replace the import statement in a \_\_main\_\_ file as follows:
```
-from reference.wiktionary import Wiktionary
+from .reference.wiktionary import Wiktionary
```
This change was based on a fixer that “detects sibling imports and converts them to relative imports” (see [Python documentation](https://docs.python.org/2/library/2to3.html#2to3fixer-import)). An explanation for this fixer can be found in the [Intra-package References](http://docs.pytho.org/3.3/tutorial/modules.html#intra-package-references) article, which states, “Note that relative imports are based on the name of the current module. Since the name of the main module is always”\_\_main\_\_“, modules intended for use as the main module of a Python application must always use absolute imports.”
Absolute imports are explained by [PEP 328](https://www.python.org/dev/peps/pep-0328/#rationale-for-absolute-imports), which states, “In Python 2.4 and earlier, if you’re reading a module located inside a package, it is not clear whether import foo refers to a top-level module or to another module inside the package. … foo will always be a module or package reachable from sys.path. This is called an absolute import.”
## See also
- [Nightmare with relative imports, how does pep 366 work?](http://stackoverflow.com/questions/2943847)
- [Using modules in main.py](http://stackoverflow.com/questions/3411293/using-modules-own-objects-in-main-py) |
| Shard | 179 (laksa) |
| Root Hash | 13739366623962901379 |
| Unparsed URL | com,philipmolloy!/import.html s443 |