4 Tips to Organize multiple AI projects in Python
If you are involved in several tasks at work, you probably get messy code and nested projects after a while. To avoid this happening, I suggest 4 useful things to do.
1- Create an Internal Utility python package
2- Create project as submodules
3- Create Debug directory
4- Use Parser for the arguments
Tip 1: Internal Utility python package
In every AI or Data Science project, some functionalities are shared in multiple tasks. It is typical to create one or more utility files and add such functions there. But after a while, these utility files become cluttered, dirty, and full of unrelated functions. Besides, it is not easy to share them with others in your team or call them in other repositories.
My solution to avoid this is to create a separate utility project, package it up and install it internally. The advantages of this package are:
- each file contains functions or classes in the same context,
- more convenient to share with others,
- it is simple to call the functions;
To create such package
Go to the desired directory, let’s say, in C:Userscurrent_user, create a new directory called utils, for example, and open it as a new Pycharm project. Consider the following one-level Python package structure.
After creating such a package, you can install it by going to the package directory, cd C:Userscurrent_usersutils in the terminal, and saying pip install -e..
After installing this package, import it, import utils, and call the functions by utils.func().
Note: if you are familiar with Python packaging, read this tutorial, or look at this one-level package as an example.
In this example, assume euclidean_distance() is a function in distances.py, and it is imported in the __init__.py, by from distances import euclidean_distance(). Then, in the main project, import utils and call utils.euclidean_distance().
Tip 2: Projects as submodules
When you are involved in several tasks, sometimes you need to use a customized function in one project for another project. Other than that, sometimes, you need to do a similar procedure for multiple projects, for example, hyperparameter optimization.
If you create a separate Pycharm or VS Code project per task, you end up with either repeated codes or disorganized projects. On the other hand, considering each project as a sub-module helps in two ways:
- You stop copy-pasting code snippets!
- You can import functions in a project into another project.
For example, suppose you are assigned to two projects; fraud detection and sales forecasting. The structure of the projects as submodules is:
In the above structure, for example, you can call train() from train_scale_forecating.py into hp_opt.py by
from sale_forecasting.train_scale_forecating import train
Note: The __init__.py is mandatory for each sub-module. However, they could be left empty. Read more here.
Tip 3: Debug directory
Testing, debugging, and visualization are the linked steps in almost every AI project, aren’t they? Most of such files are meant to be temporary or customized for the short-term goal. That’s why they should not be part of the project sub-modules.
I recommend creating a directory called “debug” and treating it as a cellar of your home, a place to put all temporary or not-wanted files. For example:
Tip 4: Parser for the arguments
In the main files, in which you might run training or evaluation, usually, lots of arguments need to be adjusted, such as learning rate, data path, etc. Using argparse, you can create an arguments parser functions and easily adjust the hyperparameters or arguments for the run or main function.
Not only it helps to have clean and readable code, but it also makes it possible to call the run function in the command line. For example, to run the main function in the below file, cd to the directory where the run file is and say python train_evaluate_fraud_detection.py --lr = 0.01. Learn more here.
Wrap up:
In this tutorial, I shared tips to have organized and clean code or repository. You might find some or all the tips simple, which I believe they really are, but still handy.
Join Medium to read great tutorials and stories!
I write Machine Learning, Deep Learning and Data Science tutorials. Upgrade to read more.
More content at PlainEnglish.io.
Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.
Build awareness and adoption for your tech startup with Circuit.


