E2E Python solution in DevOps – Part 1: Basic Python Code

In first days of 2019 I would like to start a three-parts serie about E2E DevOps solution in Python as a new year gift.

In this serie I show you how you can create a PyPi ready solution which is stored on GitHub and has fully automatized build-test-deploy CI/CD pipeline in Azure DevOps. Furthermore you will be able to use it from command line like azure-cli or aws-cli. It is quite cool, isn’t it.

Table of content


Let’s start. Today we are working with the basic Python Code. We are preparing for it on Git for later usage.

Preparation

Very first step to prepare our project which meets with Pypi project requirements. Luckily Pypi helps us to make a great project. For more information you can use this link.

Nevertheless you can use a well prepared project which is available in my Git repository. https://github.com/the1bit/pypi-project

Additionally I would like to explain the required files and folders inside this project.

Directories

We need here only some directories:

  • Root directory: this is the main directory or project directory which contains any other directories and files.
    • docs: this contains the documentations files and license file.
    • pyproject: Modiles directory where we can put any modules (python files as modules)

Files

root directory
  • .gitignore: Required for development only
  • MANIFEST.in: contains list of files which are required or not enabled in this project. This helps us to protect our project.
  • pypr: Main file for Linux bash. This file calls our python project when type pypr to Linux command-line.
  • pypr.bat: Main file for Windows command-line. This file calls our python project when type pypr to Windows command-line or PowerShell window.
  • pypr.completion.sh: This files helps to register pypr file in Linux..
  • setup.cfg: You can define special user environment related configuration options.
  • setup.py: Setup, installation and package options for our project. It requires for package information on Pypi. Please check it line by line to represents your projects.
docs directory
  • LICENSE.md: Contains your product related license information. We will use it in setup.py.
  • readme.md: Documentation of your project. Format is similar than a readme.md.
pypi directory

This directory contains only one file.

requirements.txt: List of required python packages for CI/CD pipeline.

modules (pypiproject) directory

  • __init__.py: This is a very important file. This manages the absolute modular import. – There should be a init.py file(usually kept empty) in a directory. When a python scripts need to look down into the hierarchical level, the existance of init.py file will lead the program flow. Otherwise β€œpackage not found error” occurs.
  • __main__.py: This is a very important file. This is the entry point of our project. This file contains the required parameter lists and the main calls for the other modules.
  • version.py : I use this file for versioning on project level.
core (module) directory

When you would like to implement new module you should merely copy the module file (python script) to pypiproject directory then manage the usage of that module from entry point file.

  • __init__.py: This is a very important file. This manages the absolute modular import. For futher usage it could contain some namespace related configuration.
  • pypiproject_core.py: Main module file. This should be imported in __main__py file in root of pypiproject directory. Name of the file must be contained the name of module (pypiproject) directory.

That’s all. You have done with preparation.

Upload to Git

You have left only one step. This is to upload your project to your Git repository.

# Add files to git branch
git add .
# Commit changes
git commit -m "Add my first project"
# Upload files to remote
git push <REMOTENAME> <BRANCHNAME>

It’s easy…and next time we continue with CI/CD pipeline creation. I am waiting for you… πŸ™‚

Leave a comment

Positive SSL