Skip to main content

Timeline for What is __init__.py for?

Current License: CC BY-SA 4.0

15 events
when toggle format what by license comment
May 11, 2022 at 13:20 comment added João Vitor Barbosa All I get is ImportError: attempted relative import with no known parent package. My structure: /PyToHtml init.py pytohtml.py test.py where test.py has: from .pytohtml import HTML
Feb 2, 2021 at 5:55 review Suggested edits
Feb 2, 2021 at 9:55
May 8, 2019 at 4:17 history edited smci CC BY-SA 4.0
answer was 7 years out of date, finally update it
Apr 11, 2019 at 7:16 history edited flying sheep CC BY-SA 4.0
Actual documentation, not tutorial
S Feb 5, 2016 at 16:23 history suggested daneel CC BY-SA 3.0
Distinguish name of the directory "string" from the rest of the text.
Feb 5, 2016 at 15:11 review Suggested edits
S Feb 5, 2016 at 16:23
Mar 24, 2015 at 12:39 history rollback LittleBobbyTables - Au Revoir
Rollback to Revision 2
S Mar 22, 2015 at 13:25 history suggested SHernandez CC BY-SA 3.0
Insert more information from the comments to make it more obvious and complete
Mar 22, 2015 at 12:21 review Suggested edits
S Mar 22, 2015 at 13:25
Jun 5, 2014 at 9:42 history edited Smi CC BY-SA 3.0
added 3 characters in body
Mar 7, 2014 at 21:03 comment added Two-Bit Alchemist @CarlG Try this. Make a directory called 'datetime' and in it make two blank files, the init.py file (with underscores) and datetime.py. Now open an interpreter, import sys, and issue sys.path.insert(0, '/path/to/datetime'), replacing that path with the path to whatever directory you just made. Now try something like from datetime import datetime;datetime.now(). You should get an AttributeError (because it is importing your blank file now). If you were to repeat these steps without creating the blank init file, this would not happen. That's what it's intended to prevent.
Mar 7, 2014 at 20:56 comment added Two-Bit Alchemist @CarlG Python searches a list of directories to resolve names in, e.g., import statements. Because these can be any directory, and arbitrary ones can be added by the end user, the developers have to worry about directories that happen to share a name with a valid Python module, such as 'string' in the docs example. To alleviate this, it ignores directories which do not contain a file named _ _ init _ _.py (no spaces), even if it is blank.
Jan 25, 2014 at 4:43 comment added Carl G What does this mean: "this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path"?
Jan 19, 2009 at 21:56 vote accept Mat
Jan 15, 2009 at 20:13 history answered Loki CC BY-SA 2.5