Create a project specific module in Python

Project specific module

  1. In venv-packages create a new folder “my_fx”
  2. In the my_fx folder, create a new file called “init.py”
  3. Create a new .py script that will contain your functions e.g. utilities.py, tables.py
  4. Define Python functions within the scripts
  5. Import functions using the <from [folder].[script] import [function]> framework
# Example function to format p-values in a column of a pandas data frame
# defined in a script named utilities.py
def format_pval_df(pvals):
    """
    Format a pandas series of p-values.
    - If p < 0.0001, return "<0.0001"
    - Else, format to specified number of digits

    Parameters:
    pvals : pandas Series or array-like
    digits : int, number of decimal digits to display

    Returns:
    Formatted pandas Series of strings
    """
    digits=4

    return pvals.apply(lambda p: "<0.0001" if p < 0.0001 else f"{p:.{digits}f}")
# Load the custom function
from my_fx.utilities import format_pval_df

Shared module across different projecs

Instead of storing the module in a project-specific venv, it can be placed in a more general location. This allows the module to be installed into multiple virtual environments.

  1. Follow the steps outlined in project specific module, but place the directory in another foler. The directory structure should look like this.
my_fx/
├── my_fx/
│   ├── __init__.py
│   └── utilities.py 
├── setup.py
  1. Add a setup.py file to the root directory of the module directory with the following contents
from setuptools import setup, find_packages

setup(
    name="my_fx",
    version="0.1",
    packages=find_packages(),
)
  1. Activate the Python environment where the module is to be installed in a terminal window.
  2. Navigate to the parent my_fx directory. The one containing the setup.py file.
  3. Install the modul with pip.
pip install -e .
  1. Import functions in the same framework as above <from [folder].[script] import [function]>.