-2

I'm fairly new to python (still in the first few chapters of an introductory book) and i was wondering, once i have defined a function and saved it in script mode, and i want to use it in another script do i have to define it the new program?

To clarify suppose a create a function multiplybyfive(x) which takes an input and x and returns 5x. Now suppose i create a program which uses multiplybyfive(x) in it, do i have to define it again in this new program or can i simply call it somehow?

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
Pavan Sangha
  • 249
  • 1
  • 3
  • 10
  • [you can import it](https://docs.python.org/3/reference/import.html) look at the docs – John Ruddell Oct 07 '15 at 18:43
  • 1
    Read this, it should help: http://stackoverflow.com/questions/20309456/how-to-call-a-function-from-another-file-in-python – idjaw Oct 07 '15 at 18:44
  • Thanks, i tried this in the idle mode import multiplybyfive(x) and it came up with an error – Pavan Sangha Oct 07 '15 at 18:44
  • 1
    you need a from. `from path/to/file import multiplybyfive` then later you can use it like this. `multiplybyfive(x)` – John Ruddell Oct 07 '15 at 18:46
  • @ john Ruddell do i write it exactly how you have? I tried from path/to/file import multiplybyfive and it returns invalid syntax – Pavan Sangha Oct 07 '15 at 18:48
  • @PavanSangha no. path/to/file is the location of your file. like if its on your desktop for mac then it would be like `/Users/computer_name/Desktop/math_file import multiplybyfive` where computer_name is the name of your computer and math_file is the name of the file that has your math function – John Ruddell Oct 07 '15 at 18:53
  • 1
    @PavanSangha I would advise you to continue **reading the book**, rather than asking here. – Ajean Oct 07 '15 at 18:55
  • I saved it in a file called multiply.py and i tried---------------------------------- from multiply import multiplybyfive Traceback (most recent call last): File "", line 1, in from multiply import multiplybyfive ImportError: No module named 'multiply' – Pavan Sangha Oct 07 '15 at 18:55
  • @PavanSangha where is multiply.py saved? – John Ruddell Oct 07 '15 at 18:56
  • I have a folder in my desktop of scripts of python programs which i have saved – Pavan Sangha Oct 07 '15 at 18:57
  • ok what is the exact path to your desktop from the root folder? – John Ruddell Oct 07 '15 at 18:57
  • sorry i don't understand what you mean by path – Pavan Sangha Oct 07 '15 at 20:42
  • @PavanSangha your desktop is a folder. it has a parent folder. you have to figure out what your parent folder is until you get to the root folder – John Ruddell Oct 07 '15 at 20:50
  • ok, so the path according to your definition is favourites>>>desktop, so i guess the root folder is the favourites folder? – Pavan Sangha Oct 07 '15 at 20:53

2 Answers2

0

You can import the python file which contains function multiplybyfive(x) into your new file.

For example, if you write multiplybyfive(x) in file mathfunction.py file. You can use it in a new program by

from mathfunction import multiplybyfive

x = 1
y = multiplybyfive(x)
print y
Hao Lyu
  • 176
  • 1
  • 5
  • Ok thanks, I've tried using that in IDLE mode but it doesn't work, does that just work for script mode? – Pavan Sangha Oct 07 '15 at 18:52
  • @PavanSangha you cant just copy paste an import. it has to be a folder path from your computer. look at where the folder is and figure out what your path is – John Ruddell Oct 07 '15 at 18:54
0

Why would you want to define it again? Most languages don't work like this. Consider the following layout:

src:
|-- some_module_with_function_def.py
|-- some_other_module_to_call_function.py

You can have the function definition in some_module_with_function_def.py and you can then use it in some_other_module_to_call_function.py just by doing this:

from some_module_with_function_def import multiplybyfive

x = multiplybyfive(10)
Rafay
  • 6,108
  • 11
  • 51
  • 71