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

Posted on 2014-08-27
Last Modified: 2014-08-27
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.
Question by:Evan Cutler
    LVL 28

    Accepted Solution

    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

    LVL 9

    Author Comment

    by:Evan Cutler
    awesome.  If the import(?) is not in the same folder as the I import folder/name?
    LVL 28

    Expert Comment

    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.
    LVL 2

    Assisted Solution

    As additional note, you can import all functions/entities from that file with
    from myutils import *

    Open in new window

    LVL 9

    Author Closing Comment

    by:Evan Cutler
    Thank you so much guys.
    I have alot more questions, but that requires more points. ;)

    Thanks again.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Suggested Solutions

    A short article about a problem I had getting the GPS LocationListener working.
    This is about my first experience with programming Arduino.
    Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now