Skip to main content

A sample Python application with CLI entry point

Project description

Python Sample App

Python Sample App is a starter template for new python applications. You can clone it, edit some basic stuff and get started on your new app.

Installing

First set some variables:

your_project_name=<project_name>
your_main_package_name=<main_package_name>

Then clone or download this repository:

$ git clone https://github.com/becosta/python-sample-app.git
$ rm -rf python-sample-app/.git/

Or:

$ curl -LkSs https://api.github.com/repos/becosta/python-sample-app/tarball | tar -xvzp -C . ; mv becosta-python-sample-app-* python-sample-app

Next thing to do is to rename the project directory (python-sample-app) to your project's name.

$ mv python-sample-app ${your_project_name} && cd ${your_project_name}

Then you can move on to renaming the main package directory (python_sample_app).

$ mv python_sample_app ${your_main_package_name}

Application layout

Your new application layout is as following:

python-sample-app
├── apidoc/
|   └── .gitkeep
├── dist/
|   └── .gitkeep
├── doc/
|   └── .gitkeep
├── python_sample_app/
|   ├── test/
|   |   └── __init__.py
|   ├── __init__.py
|   └── main.py
├── scripts/
|   └── .gitkeep
├── .gitignore
├── LICENSE.md
├── README.md
└── setup.py

Editing to match your project needs

After this you need to edit setup.py: $ editor setup.py

name='<your_project>',
version='<your_project_version>',
description='<your_project_description>',
author='<your_name>',
author_email='<you@example.com>',
license="<your_project_license>",
url="<your_project_url>",
packages=['<your_project_main_package>'],
entry_points={
        'console_scripts': [
            '<your_command>=<your_project_main_package>.main:main',
        ],
},

Note that <your_command> is the name of the entry script that will be generated by $ pip install -e .. In other words this is the name of the command you'll use to call your application from the console later on. Also, note that you can have more than one entry point see the setuptools documentation for more information.

Once done, edit LICENSE.md, README.md and .gitignore according to your needs. If you don't need any of the apidoc, dist, doc and scripts directories you should remove them now.

Finally you can create your git repository, install your project locally and run it:

$ git init && git add .
$ git commit -m "Iniatial commit"
$ pip install -e .
$ <your_command>
Hello World!
$

Start coding

To get started, create a module in ${your_project_name}/${your_main_package_name} and call it in ${your_project_name}/${your_main_package_name}/main.py.

A simple example would be :

## ${your_project_name}/${your_main_package_name}/application.py
def class Application:
    def run(self):
        print("Hello World! From module application.")


# ${your_project_name}/${your_main_package_name}/main.py
import <your_main_package_name>.application
def main():
    <your_main_package_name>.application.Application().run()

You can then add more modules, either as simple as a flat file like application.py or more complex with a full directory tree.

Distributing your project

You can easily distribute your project to PyPi

Once you have created an account on PyPi, a pretty basic example would be:

pip install twine
python setup.py sdist
twine upload dist/*

See the PyPi tutorial for more information

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_sample_app_mackes-1.0.14.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_sample_app_mackes-1.0.14-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file python_sample_app_mackes-1.0.14.tar.gz.

File metadata

File hashes

Hashes for python_sample_app_mackes-1.0.14.tar.gz
Algorithm Hash digest
SHA256 19b55d8cbff30d4a9df8ba98573f2f0462db088fde1ab53f50c58d742789278b
MD5 637b9e47fcd67d7a2d3068d58ed2995c
BLAKE2b-256 31f8dd5ee3c051a79d8941cc3ce7963ffb3bdd32fcd9548db74b8184c965d209

See more details on using hashes here.

File details

Details for the file python_sample_app_mackes-1.0.14-py3-none-any.whl.

File metadata

File hashes

Hashes for python_sample_app_mackes-1.0.14-py3-none-any.whl
Algorithm Hash digest
SHA256 900b2030f9333ae473cfbb3abf1e381b0d3b20fa52ec7262a74bf97d0f0f72f5
MD5 9e11f7268c9e73d11b3edfbce980186e
BLAKE2b-256 7e49b8ec4d1641a7cd93b7b56834927c9eb126b85877396648571bb98d626738

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page