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.
    • modules: here 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.
  • pypi-project: Entry point of our project. It must have been same name than our project. When we type our package name to command line this script will be executed. You mustn’t use file extension.
  • 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.txt: Contains your product related license information. We will use it in setup.py.
  • readme.rst: Documentation of your project. Format is similar than a readme.md.
modules 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.
  • version.py : I use this file for versioning on project level.

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

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