1

I have a Python function in a file called func.py

def calc(x,y):
    return x*y

I have to import this function while working in another file called mytask.py

How can I import the function?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Roman
  • 3,007
  • 8
  • 26
  • 54

1 Answers1

3

Save the file in the same directory and then have a statement

from func import calc

To import from a different directory

  1. Add the file to a new directory, call it funcs
  2. Create a file __init__.py and have a line __all__ = ['calc']
  3. Import as from funcs import func

Reference

Community
  • 1
  • 1
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140