python - What is for?

ID : 194

viewed : 160

Tags : pythonmodulepackagepython-packagingpython

Top 5 Answer for python - What is for?

vote vote


It used to be a required part of a package (old, pre-3.3 "regular package", not newer 3.3+ "namespace package").

Here's the documentation.

Python defines two types of packages, regular packages and namespace packages. Regular packages are traditional packages as they existed in Python 3.2 and earlier. A regular package is typically implemented as a directory containing an file. When a regular package is imported, this file is implicitly executed, and the objects it defines are bound to names in the package’s namespace. The file can contain the same Python code that any other module can contain, and Python will add some additional attributes to the module when it is imported.

But just click the link, it contains an example, more information, and an explanation of namespace packages, the kind of packages without

vote vote


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

mydir/spam/ mydir/spam/ 

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 

based on this

vote vote


In addition to labeling a directory as a Python package and defining __all__, allows you to define any variable at the package level. Doing so is often convenient if a package defines something that will be imported frequently, in an API-like fashion. This pattern promotes adherence to the Pythonic "flat is better than nested" philosophy.

An example

Here is an example from one of my projects, in which I frequently import a sessionmaker called Session to interact with my database. I wrote a "database" package with a few modules:


My contains the following code:

import os  from sqlalchemy.orm import sessionmaker from sqlalchemy import create_engine  engine = create_engine(os.environ['DATABASE_URL']) Session = sessionmaker(bind=engine) 

Since I define Session here, I can start a new session using the syntax below. This code would be the same executed from inside or outside of the "database" package directory.

from database import Session session = Session() 

Of course, this is a small convenience -- the alternative would be to define Session in a new file like "" in my database package, and start new sessions using:

from database.create_session import Session session = Session() 

Further reading

There is a pretty interesting reddit thread covering appropriate uses of here:

The majority opinion seems to be that files should be very thin to avoid violating the "explicit is better than implicit" philosophy.

vote vote


There are 2 main reasons for

  1. For convenience: the other users will not need to know your functions' exact location in your package hierarchy (documentation).

    your_package/     ... 
    # in from file1 import * from file2 import * ... from fileN import * 
    # in def add():     pass 

    then others can call add() by

     from your_package import add 

    without knowing file1, like

     from your_package.file1 import add 
  2. If you want something to be initialized; for example, logging (which should be put in the top level):

     import logging.config  logging.config.dictConfig(Your_logging_config) 
vote vote


The file makes Python treat directories containing it as modules.

Furthermore, this is the first file to be loaded in a module, so you can use it to execute code that you want to run each time a module is loaded, or specify the submodules to be exported.

Top 3 video Explaining python - What is for?