How to manage a python file with functions

my question is simple and I am not sure it is the right place to ask it…
I have a file with functions that I would like to use in a steamlit app ie:
the structure of my program is as follow: 
└─── pages/

First question:
How is it possible to have this program in the app ? How to β€œload” it in testing mode ie while I use streamlit run β€œ/Users/jacques/Library/Mobile Documents/com~apple~CloudDocs/Projets/Quantalys/” ?

Second question:
in the deployment phase, how to have this in GitHub later and use it ? what should be the directory to use


Hi @Jacques2101,

Thanks for posting!

One question, is the a utility function that you’re then importing into one of the pages or

If yes, you will have to create a folder within the root directory and move the file into it. Also don’t forget to add an empty file in the same folder. This makes that folder (say utils) a Python package.

β”œβ”€β”€ utils
β”‚   β”œβ”€β”€
β”‚   └──
└── pages

When you’re ready to deploy your app, your GitHub repo should mimic your local project structure.

what should I put inside program (double _ ?) ? Is it empty ?

Then to use the functions inside should I do import utils inside,,…


The file should be empty. Its purpose is to signal to Python that the directory it’s in should be treated as a Python package which subsequently allows you to import from modules (functions) in that directory.

For importing example, in your , , and any other file where you need to use functions from, you would use:

  • import utils.func to import the whole module, or
  • from utils.func import specific_function to import a specific function within your

Here’s a good article that you can read up more on this: 3 Techniques to Effortlessly Import and Execute Python Modules | by Khuyen Tran | Towards Data Science

Thanks a lot !

1 Like

Glad you found it helpful.Happy Streamlit-ing! :balloon:

one last question !

when I do:

import utils.func

how to call a function whose name is test(a,b) ? I tried:


but none of them works


So when you import a module in Python, in this case import utils.func, you need to use the module name as a prefix when calling functions from that module. In your case, since test is a function inside which is in the utils folder, you should call it using utils.func.test(a,b). If this is not working, double-check that you don’t have any typos in the functions or import statement.

It should look like so somewhere in your code:

import utils.func

result = utils.func.test(a, b)

Please feel free to share a link to your repository with the full code so I can troubleshoot it better for you.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.