how do I use python functions in a separate .py file?

I'm writing a python script for someone as my first attempt in learning python.

The script is getting long, and I want to include functions.
But instead of putting all the functions in the front of the same file, I was wondering if there was a way to put it in a different file and refer to that function from the main .py file.

Thanks much.
Evan CutlerVolunteer Chief Information OfficerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Just put the definitions of the functions into the separate file with the .py extension, and (for simplicity) place that file in the same directory as your main script file:
def fn():
    print('fn called')

Open in new window

Your script imports the module (use the file name without extension).
import myutils


Open in new window

And that is all. The module name must be used as the part of the name of the function inside. Or you can import the specific function from the module and use it without the module name:
from myutils import fn


Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Evan CutlerVolunteer Chief Information OfficerAuthor Commented:
awesome.  If the import(?) is not in the same folder as the I import folder/name?
No. The import is more abstract (see for various forms of the statement). Think about it similarly as about searching executables in OS using the PATH variable. Python has some rules where to search for the file of the name. Actually, there is the sys module that implements sys.path as list of directories where the modules are searched (see

You can print the sys.path, and you can modify it if you need.

If you need to implement some hierarchy of modules, it is usually done via packages ( that differ from a simple directory hierarchy. I suggest to read the doc or simply leave it out for now.
As additional note, you can import all functions/entities from that file with
from myutils import *

Open in new window

Evan CutlerVolunteer Chief Information OfficerAuthor Commented:
Thank you so much guys.
I have alot more questions, but that requires more points. ;)

Thanks again.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.