In the my_fx folder, create a new file called “init.py”
Create a new .py script that will contain your functions e.g. utilities.py, tables.py
Define Python functions within the scripts
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.pydef 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=4return pvals.apply(lambda p: "<0.0001"if p <0.0001elsef"{p:.{digits}f}")
# Load the custom functionfrom 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.
Follow the steps outlined in project specific module, but place the directory in another foler. The directory structure should look like this.