How do I call a function from another .py file?

Question: contains a function named function. How do I import it?

from import function(a,b)

The above gives an error:

ImportError: No module named ‘’; file is not a package

Asked By: user2977230



First, import function from

from file import function

Later, call the function using:

function(a, b)

Note that file is one of Python’s core modules, so I suggest you change the filename of to something else.

Note that if you’re trying to import functions from to a file called, you will need to make sure that and are in the same directory.

Answered By: Games Brainiac

Do not write .py when importing.

Let contain some functions inside it:

def f():
  return 1

def g():
  return 2

To import these functions into, do this:

from file_a import f, g
Answered By: Salvador Dali

First save the file in .py format (for example,
And if that file have functions,

def xyz():



def abc():



In the calling function you just have to type the below lines.




a =

b =


Answered By: Nagaraj Simpi

You should have the file at the same location as that of the Python files you are trying to import. Also ‘from file import function’ is enough.

Answered By: Girish M

You don’t have to add

Just keep the file in the same location with the file from where you want to import it. Then just import your functions:

from file import a, b
Answered By: Mohan

Method 1. Import the specific function(s) you want from

from file import function

Method 2. Import the entire file:

import file as fl

Then, to call any function inside, use:

fl.function(a, b)
Answered By: Pulkit Bansal

Suppose the file you want to call is and the method you want to call is method1, then first import the file and then the method

from anotherfile import method1

if method1 is part of a class, let the class be class1, then

from anotherfile import class1

then create an object of class1, suppose the object name is ob1, then

ob1 = class1()
Answered By: Amir Md Amiruzzaman

You can call the function from a different directory as well, in case you cannot or do not want to have the function in the same directory you are working. You can do this in two ways (perhaps there are more alternatives, but these are the ones that have worked for me).

Alternative 1
Temporarily change your working directory

import os

os.chdir("**Put here the directory where you have the file with your function**")

from file import function

os.chdir("**Put here the directory where you were working**")

Alternative 2
Add the directory where you have your function to sys.path

import sys

sys.path.append("**Put here the directory where you have the file with your function**")

from file import function
Answered By: Juan Ossa

Inside MathMethod.Py.

def Add(a,b):
   return a+b 

def subtract(a,b):
  return a-b

Inside Main.Py

import MathMethod as MM 


Answered By: Mahabubuzzaman

To fix

ModuleNotFoundError: No module named

try using a dot (.) in front of the filename to do a relative import:

from .file import function
Answered By: Ricky Boy

Rename the module to something other than ‘file’.

Then also be sure when you are calling the function that:

1)if you are importing the entire module, you reiterate the module name when calling it:

import module


import pizza

2)or if you are importing specific functions, functions with an alias, or all functions using *, you don’t reiterate the module name:

from pizza import pizza_function


from pizza import pizza_function as pf


from pizza import *
Answered By: misterrodger

in my case i named my file and couldn’t make it work until i changed to

Answered By: bresleveloper

If your file is in the different package structure and you want to call it from a different package, then you can call it in that fashion:

Let’s say you have following package structure in your python project:

Python package and file structure

in – python file you have some function, like:

def add(arg1, arg2):
    return arg1 + arg2

def sub(arg1, arg2) :
    return arg1 - arg2

def mul(arg1, arg2) :
    return arg1 * arg2

And you want to call different functions from, then following way you can do it:

Define import statement in – file for import all function

from import *

or define each function name which you want to import

from import add, sub, mul

Then in you can call function for execute:

num1 = 20
num2 = 10

print("n add : ", add(num1,num2))
print("n sub : ", sub(num1,num2))
print("n mul : ", mul(num1,num2))


 add :  30

 sub :  10

 mul :  200
Answered By: abanmitra

append a dot . in front of a file name if you want to import this file which is in the same directory where you are running your code.

For example, I’m running a file named and I want to import a method named addFun which is written in, and is there in the same directory

from .b import addFun
Answered By: Shravan Kumar

Functions from .py file (can (of course) be in different directory) can be simply imported by writing directories first and then the file name without .py extension:

from directory_name.file_name import function_name

And later be used: function_name()

Answered By: Weky

Any of the above solutions didn’t work for me. I got ModuleNotFoundError: No module named whtever error.
So my solution was importing like below

from . import filename # without .py  

inside my first file I have defined function fun like below

# file name is
def fun():
  print('this is fun')

inside the second file lets say I want to call the function fun

from . import firstFile

def secondFunc(): # calling `fun` from the first file

secondFunc() # calling the function `secondFunc` 
Answered By: noone

in my main script file i need call passGen function which generate password hash and that functions is under

The quickest and easiest solution for me is

Below is my directory structure

enter image description here

So in i have import my function with below syntax

from modules.passwordGen import passGen

enter image description here

Answered By: Mansur Ul Hasan

Just a quick suggestion,
Those who believe in auto-import by pressing alt+ enter in Pycharm and cannot get help.

Just change the file name from where you want to import by:
right-clicking on the file and clicking on refactor-> rename.
Your auto-import option will start coming up

Answered By: Aniket Malik

Solution1: In one file define any function(s).

# functions
def Print_Text():
    print( 'Thank You')

def Add(a,b):
    return c 

In the other file:

#Import defined functions
from myfun import *

#Call functions

Solution2: if this above solution did not work for Colab

  1. Create a foldermyfun
  2. Inside this folder create a file
  3. Write all your functions in
  4. Import your functions from Colab notebook from myfun import *
Answered By: ASE
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.