Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python File Structure on GitHub

Tags:

python

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?

like image 214
user1427661 Avatar asked Mar 06 '13 19:03

user1427661


People also ask

Can I write Python code in GitHub?

To publish your Python project on GitHub: Create a GitHub account if you don't already have one. Create a new repo for your project. Click on the “+” menu next to your avatar in the upper right of the page and choose “New repository”.

What is file structure in Python?

In python, the file system contains the files and directories. To handle these files and directories python supports “os” module. Python has the “os” module, which provides us with many useful methods to work with directories (and files as well).


1 Answers

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.

like image 194
poke Avatar answered Oct 15 '22 01:10

poke