Structuring a Python project

I recently needed to structure a Python project for distribution. I based the structure and approach of my project’s repository on Google PyBadges

My project was portbalance, a simple stock portfolio management and rebalancing library, which puts in calls to the Tiingo API in order to pull historical stock data. For this reason, my project needed to store an API key that would not actually be distributed with the project.

The project structure is:

├── portbalance
│	├──
│	├──
│	├──
│	└── assets
│		└── keys.txt
├── demos
│	└── demos.ipynb
├── resources
│	└── sample_portfolio.txt
├── tests
│	└──
├── .gitignore

Structuring with global variables

For my project I had a private API key that was stored locally on my computer. I first protecting this by adding the file name to .gitignore . However, since this is a non-Python file that I want linked to the installed package locally, I added a file to my project’s base directory, in order to ensure that collected this file. I also added the line include_package_data=True, in

Because I wanted the API key to become a global variable accessible to multiple files in the project, I added a file that figured out all of the global variables. This file was then run at the top of every other project file that needed access to those variables, using from .config import _API_KEY

I also had an file that basically just imported the package

I based this approach on these resources:

Adding tests

  • I used the unittests package, since it is built-in to most python distributions, and it can accommodate doctests

Running unittest with typical test directory structure

Pytests also looks really nice: