__init__.py files
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: __init__.py files

  1. #1
    Join Date
    Sep 2007
    Posts
    62

    __init__.py files

    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?

    Thanks.

  2. #2
    Join Date
    Aug 2009
    Posts
    1

    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.

  3. #3
    Join Date
    Oct 2011
    Posts
    1

    Re: __init__.py files

    By the time I managed to register here, I kind of forgot the answer but let me restate first:

    (http://docs.python.org/release/2.1.3...00000000000000)
    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.

    When you
    >>> 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
    __init__.py
    wav.py
    string/
    english.txt
    deutsch.txt
    Effects/ Subpackage for sound effects
    __init__.py
    echo.py
    surround.py
    string/
    english.txt
    detusch.txt

    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
    >>> os.environ['PYTHONPATH']
    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__
    KeyError: 'PYTHONPATH'

    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
    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
    >>> sys.path
    ['', '/usr/local/lib/python2.3', '/usr/local/lib/python2.3/plat-linux2',
    '/usr/local/lib/python2.3/lib-tk', '/usr/local/lib/python2.3/lib-dynload',
    '/usr/local/lib/python2.3/site-packages']
    >>>

    'The null string in sys.path represents the current working directory'

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center