-3

I'm new to Python, coming from Matlab like many. I'm used to defining my functions as standalone .m files and calling them easily from a second script as long as the function is saved somewhere within the defined Matlab path.

I've learned how to define a (user-defined) function in Python (def my_function() etc.), but I'm coming up short in my Google searches on a way to define the function in a a separate .py file A, and how to call it in another script B. All help files I can find give support on how to define the function within the same script. When I try calling the function (which I've defined as a .py file in the same folder I'm using for script A) my script doesn't recognize it. Do I need to be declaring the function at the beginning of my script? I'm getting the sense that Python is very different in the way it handles these things than Matlab--perhaps I can't do this?

Cheers

  • its not the pythonic way of doing things, but you can drop a bunch of functions in a file, say `funcs.py` in the same folder as the main script. then in the main script you do `import funcs` now you can do `func.my_func()` etc... – Nullman Feb 13 '17 at 19:54
  • You should read the [official tutorial](https://docs.python.org/3/tutorial/modules.html). – mkrieger1 Feb 13 '17 at 19:58

1 Answers1

0

You have to use imports

for example, you have the function doSomething() in functions.py and want to work in main.py

in main.py

from functions import doSomething

var = doSomething()
Alexev
  • 157
  • 2
  • 19