What is ?

Okay yet another useful post. This post is really important and useful for anyone just starting out with python. So what is the file ?

Files name are used to mark directories on disk as Python package directories. If you have the files


and mydir is on your path, you can import the code in as

import spam.module


from spam import module

If you remove the file, Python will no longer look for submodules inside that directory, so attempts to import the module will fail.

The file is usually empty, but can be used to export selected portions of the package under more convenient name, hold convenience functions, etc. Given the example above, the contents of the init module can be accessed as

import spam

And finally here is what the official documentation has to say about this file:

The 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, can just
be an empty file, but it can also execute initialization code
for the package or set the __all__ variable, described later.

Source :


One thought on “What is ?

  1. Pingback: Packaging and distributing your python libraries | Bite Sized Python Tips

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s