How to manage a python file with functions

Hi,
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:

Home.py 
└─── func.py
└─── pages/
  └─── 1_Page_one.py 
  └─── 2_Page_two.py 

First question:
How is it possible to have this func.py 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/Home.py” ?

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

Thanks

Hi @Jacques2101,

Thanks for posting!

One question, is the func.py a utility function that you’re then importing into one of the pages or Home.py?

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

Your-Folder-Name
├── Home.py
├── utils
│   ├── __init__.py
│   └── func.py
└── pages
    ├── 1_Page_one.py
    └── 2_Page_two.py

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

Thanks.
what should I put inside __init__.py program (double _ ?) ? Is it empty ?

Then to use the functions inside func.py should I do import utils inside Home.py, 1_Page_one.py,…

Thanks.

The __init__.py 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 Home.py , 1_Page_one.py , and any other file where you need to use functions from func.py, 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 func.py.

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:

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

but none of them works

Thx

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 func.py 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.

1 Like

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