Python: Reusing your own modules

Posted on: 18 Jan 2026

When working on different projects in Python, I often end up writing a collection of functions that I would like to reuse in other projects. One way of doing this is to add the directory containing the Python module to the Python path using sys.path.append().

However, having these absolute paths at the beginning of every script is not particularly nice. And copying the same Python file into different project directories is probably an even worse idea, especially if you are still developing the functions.

There is a rather convenient way of doing this in Python using something called an editable install. These are mainly notes for my own reference.

Suppose I have a Python file called mytools.py containing a collection of functions that I frequently use for my analysis.

1. Setting up the package

Navigate to the directory containing the Python file. The idea is to have the following folder structure:

mytools/
├── mytools.py
└── setup.py

Create a new file called setup.py in the same directory and add the following lines.

from setuptools import setup

setup(
    name="mytools",
    version="0.1",
    py_modules=["mytools"],
)

Of course, change mytools to whatever your Python module is called.

2. Installing the package

Open the terminal or Miniforge prompt and activate the Conda environment in which you want to use the module.

conda activate your_env_name

Navigate to the directory containing the setup.py file and run:

python -m pip install -e .

The -e stands for editable, and the . refers to the current directory.

And that’s it!

3. Using the module in other projects

You can now import the module from any working directory, as long as you are using the same Python environment.

import numpy as np
import mytools as mt

And access the functions just like you would with any other Python package.

smoothed = mt.smooth(signal, window=5)

thickness = mt.measure_thickness(mesh)

No more messing around with sys.path.append()!

Why editable?

The nice thing about an editable installation is that you can continue modifying the original Python file. You don’t need to reinstall the package every time you make changes to your functions.

This is particularly useful when developing image analysis functions that are being used across several projects. You only have to maintain one copy of the code.

One small thing to keep in mind when working with Jupyter notebooks is that Python does not automatically reload an already imported module when you modify the source file.

A rather convenient way around this is to add the following lines at the beginning of your notebook.

%load_ext autoreload
%autoreload 2

This makes IPython automatically reload modified modules before executing subsequent cells. Quite handy when you are developing functions in one file and testing them in a notebook.

A note on pyproject.toml

A more modern way of setting up the module is to use a pyproject.toml file instead of setup.py.

[build-system]
requires = ["setuptools>=64"]
build-backend = "setuptools.build_meta"

[project]
name = "mytools"
version = "0.1.0"

[tool.setuptools]
py-modules = ["mytools"]

The installation command remains exactly the same.

python -m pip install -e .

For small collections of Python functions that I am constantly modifying and using across different projects, I find this workflow quite convenient. It almost feels like having your own little toolbox installed in Python!

References: