2

Please excuse what I know is an incredibly basic question that I have nevertheless been unable to resolve on my own.

I'm trying to switch over my data analysis from Matlab to Python, and I'm struggling with something very basic: in Matlab, I write a function in the editor, and to use that function I simply call it from the command line, or within other functions. The function that I compose in the matlab editor is given a name at the function definition line, and it's generally best for the function name to match the .m file name to avoid confusion.

I don't understand how functions differ in Python, because I have not been successful translating the same approach there.

For instance, if I write a function in the Python editor (I'm using Python 2.7 and Spyder), simply saving the .py file and calling it by its name from the Python terminal does not work. I get a "function not defined" error. However, if I execute the function within Spyder's editor (using the "run file" button), not only does the code execute properly, from that point on the function is also call-able directly from the terminal.

So...what am I doing wrong? I fully appreciate that using Python isn't going to be identical to Matlab in every way, but it seems that what I'm trying to do isn't unreasonable. I simply want to be able to write functions and call them from the python command line, without having to run each and every one through the editor first. I'm sure my mistake here must be very simple, yet doing quite a lot of reading online hasn't led me to an answer.

Thanks for any information!

2
  • 4
    You have to import functions! Commented Aug 17, 2015 at 13:23
  • 1
    also as bonus tip PyCharm is excellent and free IDE Commented Aug 17, 2015 at 13:27

4 Answers 4

4

If you want to use functions defined in a particular file in Python you need to "import" that file first. This is similar to running the code in that file. Matlab doesn't require you to do this because it searches for files with a matching name and automagically reads in the code for you.

For example,

myFunction.py is a file containing

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

In order to access this function from the Python command line or another file I would type

from myFunction import myAdd

And then during this session I can type

myAdd(1, 2)

There are a couple of ways of using import, see here.

Sign up to request clarification or add additional context in comments.

Comments

0

You need to a check for __main__ to your python script

def myFunction():
  pass

if __name__ == "__main__":
  myFunction()

then you can run your script from terminal like this

python myscript.py

Also if your function is in another file you need to import it

from myFunctions import myFunction
myFunction()

Comments

0

Python doesn't have MATLAB's "one function per file" limitation. You can have as many functions as you want in a given file, and all of them can be accessed from the command line or from other functions.

Python also doesn't follow MATLAB's practice of always automatically making every function it can find usable all the time, which tends to lead to function name collisions (two functions with the same name).

Instead, Python uses the concept of a "module". A module is just a file (your .py file). That file can have zero or more functions, zero or more variables, and zero or more classes. When you want to use something from that file, you just import it.

So say you have a file 'mystuff.py':

X = 1
Y = 2

def myfunc1(a, b):
   do_something

def myfunc2(c, d):
   do_something

And you want to use it, you can just type import mystuff. You can then access any of the variables or functions in mystuff. To call myfunc2, you can just do mystuff.myfunc2(z, w).

What basically happens is that when you type import mystuff, it just executes the code in the file, and makes all the variables that result available from mystuff.<varname>, where <varname> is the name of the variable. Unlike in MATLAB, Python functions are treated like any other variable, so they can be accessed just like any other variable. The same is true with classes.

There are other ways to import, too, such as from mystuff import myfunc.

2 Comments

Thanks for this; your answer really gets to the heart of my confusion, which arose because of Matlab's "1 function per file" limit that does not exist in Python. So now I'll have to figure out how to import a bunch of functions written in separate files simultaneously, as it seems like sticking with 1 function per file will (generally) be easier to debug (for me at least). But I'll work that out on my own.
There is no advantage to having one function per file. It won't make things easier to debug at all. It will only make it harder to keep things organized. Note that you can organize files into folders, using a concept called packages (which recent versions of MATLAB copied). You can put a __init__.py file (it can be empty) in folder, and you can then import that folder the same way as you would a module. Further, the __init__.py file can also import stuff, so you can have functions in the folder, and then the __init__.py pulls them in for easier access.
-2

You run python programs by running them with

python program.py

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.