June 7th, 2009, 11:35 PM
In the Python 2.5 documentation it mentions (6.4 Packages) :
"The __init__.py files are required to make Python treat the directories as containing packages; 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. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later. "
I do not understand the part : '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'
Might anyone be able to enlighten me?
August 5th, 2009, 06:53 PM
Re: __init__.py files
if you named the module "string", then references to the standard string might get redirected to your string module, making it impossible to access the built in string. The purpose of making you include and __init__ file is so that only directories you desire get treated like packages. If you had a directory string somewhere else, the compiler won't care unless it has init.py, so you won't hide anything you don't want to.
October 27th, 2011, 01:48 PM
Re: __init__.py files
By the time I managed to register here, I kind of forgot the answer but let me restate first:
6.4 Packages, §3 -
[...] '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'
I first thought I understood but after reading your question I didn't understand either.
>>> import foo.bar
foo has to be the name of a directory and unless foo contains __init__.py it is not recognized as a package. __init__.py can be empty or contain some code.
I imagine that the structure the author had in mind might have looked something like:
Sound/ Top-level package
__init__.py Initialize the sound package
Formats/ Subpackage for file format conversions
Effects/ Subpackage for sound effects
If echo.py wanted to 'import string' - the standard library one, it could not since the local directory 'string' would have priority.
From 6.1.2 The Module Search Path (http://docs.python.org/tutorial/modules.html)
'When a module named spam is imported, the interpreter searches for a file named spam.py in the directory containing the input script and then in the list of directories specified by the environment variable PYTHONPATH.'
>>> import os
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/UserDict.py", line 23, in __getitem__
Let's try http://docs.python.org/install/index...st-search-path
'When the Python interpreter executes an import statement, it searches for both Python code and extension modules along a search path. A default value for the path is configured into the Python binary when the interpreter is built. You can determine the path by importing the sys module and printing the value of sys.path.'
Python 2.2 (#11, Oct 3 2002, 13:31:27)
[GCC 2.96 20000731 (Red Hat Linux 7.3 2.96-112)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
['', '/usr/local/lib/python2.3', '/usr/local/lib/python2.3/plat-linux2',
'The null string in sys.path represents the current working directory'
Tags for this Thread
Click Here to Expand Forum to Full Width