9

I've been looking around at some open source projects on Python, and I'm seeing a lot of files and patterns that I'm not familiar with.

First of all, a lot of projects just have a file called setup.py, which usually contains one function:

setup(blah, blah, blah)

Second, a lot contain a file that is simply called __init__.py and contains next to no information.

Third, some .py files contain a statement similar to this:

if __name__ == "__main__"

Finally, I'm wondering if there are any "best practices" for dividing Python files up in a git repository. With Java, the idea of file division comes pretty naturally because of the class structure. With Python, many scripts have no classes at all, and sometimes a program will have OOP aspects, but a class by class division does not make that much sense. Is it just "whatever makes the code the most readable," or are there some guidelines somewhere about this?

3 Answers 3

7

The setup.py is part of Python’s module distribution using the distrubution utilities. It allows for easy installation of the Python module and is useful when, well, you want to distribute your project as a whole Python module.

The __init__.py is used for Python’s package system. An empty file is usually enough to make Python recognize the directory it is in as a package, but you can also define different things in it.

Finally, the __name__ == '__main__' check is to ensure that the current script is run directly (e.g. from the command line) and it is not just imported into some other script. During a Python script execution only a single module’s __name__ property will be equal to __main__. See also my answer here or the more general question on that topic.

2

The setup.py is part of distutils setup process. You'll want to have one of those if you're distributing a module instead of just a basic script (which even then it's a good idea to have one so you can easily expand into a module later).

The __init__.py part of the python module import process:

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

mydir/spam/init.py mydir/spam/module.py and mydir is on your path, you can import the code in module.py as:

import spam.module or

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

if __name == "__main__" is a way to indicate code that would be executed if the file was run directly instead of imported.

To answer on how to layout your code, the distfiles documentation has a good guide on this.

1
  • Excuse me mister but I think you made a mistake in if __name == "__main__" you forgot the __ after name. Ping me when you see this and i will remove the comment. Nov 22, 2015 at 12:48
2

In addition to @poke's answer, see this related question on what the directory structure of a python project should be. Here is another useful tutorial on how to make your project easily runnable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.