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
  • 45,811
  • 27
  • 120
  • 136
Roman
  • 2,587
  • 8
  • 23
  • 51

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
  • 45,811
  • 27
  • 120
  • 136