How to create the Structure of a Python Project

How to create the Structure of a Python Project

How to create the Structure of a Python Project - I love using Python for creating command-line applications which require just a bit more logic than writing a bash-script

Python has excellent libraries for parsing command-line arguments and running other shell commands, while at the same time you can take advantage of the powerful object-oriented language. Additionally you can verify and document your application with the Python's unit testing framework.

The Example Application for this text can be found in

Structuring your Application

In my experience the best folder structure for a Python project is to put your executables in a bin folder and your project into a your project name folder (I will use name project in this text). This way you can keep your core functionalities separated and reusable. It is also a standard for other applications.

In project folder you should have as the main access point to your application. Your functionalities should then go to lib and your unitests to tests. This way the skeleton of your project should look like:

+ bin
  - project
+ project
  + lib
  + tests

Now your application can be executed by running:

$ bin/project <parameters>

Separating Parameters, Shell Commands and Functionalities

As in all object-oriented programming you should aim to separate your concerns. It's often forgotten in Python applications because reading the command-line parameters, handling options and running other shell commands is very easy.

Parsing the Command-Line Options Create a class which defines and collects the command-line parameters. Python provides optparse, for which you can very easily define the options and behavior:

usage = 'bin/project'
parser = OptionParser(usage=usage)
parser.add_option('-x', '--example',
                  help='An example option')

Now you have created a parser which reads a target value to the example variable by running bin/project -x or bin/project --example

Running other Shell Commands If you want to create an application which depends on other shell commands you should separate the shell execution in its own class. This way your core functionalities can be easily re-used in other environments or applications, and you can more easily collect logs, errors and exceptions that come from external sources. I recommend you to use the Python subprocess for your external shell commands.

Create a classes for the process execution and the process exceptions (see process-class in the example project).

The Core Functionalities In your project/lib/ you can now implement the core functionalities. Since this is an example I've only included receiving options(which have been separated from collecting the command-line arguments) and running the date command through your Process-class. See in the example project.

Running your app Call your project's from the bin/project executable:


BINPATH=`dirname $0`
python "$BINPATH/../project/" [email protected]    

Don't forget to change the access rights for execution:

$ chmod 755 bin/project

In you then collect the command-line options and run your application:

import sys

from lib import Project
from lib import Options

if __name__ == '__main__':
    options = Options()
    opts, args = options.parse(sys.argv[1:])
    v = Project(opts)


Finally you are ready to run the application:

$ bin/project <arguments>

Testing (and Documenting)

As already written in Unit Testing in Python and Short Introduction to Unit Testing, I cannot emphasize the importance of testing enough. With unit tests you can guide the development, verify your functionalities and document the behavior of your application.

Add your tests in the project/tests folder (see tests-folder in the example project). I recommend you to use nose for running your tests. Testing Command-Line Arguments

import unittest

from lib import Options

class TestCommandLineArguments(unittest.TestCase):
    def setUp(self):
        self.options = Options()

    def test_defaults_options_are_set(self):
        opts, args = self.options.parse()
        self.assertEquals(opts.example, 'example-value')    

Running the Tests

$ nosetests
Ran 5 tests in 0.054s


Originally published by Marko Klemetti at


What's new in Bootstrap 5 and when Bootstrap 5 release date?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

Basic Data Types in Python | Python Web Development For Beginners

In the programming world, Data types play an important role. Each Variable is stored in different data types and responsible for various functions. Python had two different objects, and They are mutable and immutable objects.

Top Python Development Companies | Hire Python Developers

After analyzing clients and market requirements, TopDevelopers has come up with the list of the best Python service providers. These top-rated Python developers are widely appreciated for their professionalism in handling diverse projects. When...

Python Hello World Program - Create & Run Your First Python Program in PyCharm

Python Hello World Program - Your first step towards Python world. Learn how to create the Hello World Python program in PyCharm.

Standard Data Types in Python - Python Tutorial - Python Training

This video on 'Standard Data Types in Python' will help you establish a foothold on Python by helping you learn basic concepts.

Python Libraries You Must Learn in 2020-Learn Python-Python Training

This LIVE session on 'Python Libraries' will help you understand the topmost trending Python libraries toy must learn.