Search code examples
pythonfunctionfilecall

How to call functions from other python files


I am working on a Python Project and want to call multiple functions from other python files, but I'm having trouble. Can someone please help me?


Solution

  • Each of your Python file can act as a module, that you can import.

    for example for numpy you just type 'import numpy' and then you'll be able to use numpy.sin() function.

    by default you can only append modules located in sys.path. So What you can do is:

    import sys
    if not (<folder_with_your_function> in sys.path):
        sys.path.append( <folder_with_your_function> )
    
    import <your_function>