-3

I know this question has been asked before but I can't make heads or tails of what the answer means.

I am making the transition from MATLAB to Python. In MATLAB I can write my own functions and use them in my code. I know I can do the same thing in Python. But I am having a hard time figuring out how to do it.

What I would like to do it create a file with multiple function definitions and then import it into Python like any other module.

First, is this the proper way of thinking it about it? Or do I just need to create multiple definition files for each function?

Second, if it is the proper way of thinking about it how do I access the file? I know you have to set the PYTHONPATH. I have looked at it and where it is looking makes no sense to me.

As an Example: I created a folder called User. In it I have a python function called ted.py. I put said file where the rest of the library files are located (as in numpy or scipy). I want to import the file called User. How can I do this?

After working with Python for awhile I get it. As long as the file is in the same directory and you use the import properly you can use one , some or all of the function definitions in the file.

asked Oct 17, 2015 at 18:30
4
  • yes, you are in right track. Every py file can be imported as a module. Just write from filename import functionName and you are done. Commented Oct 17, 2015 at 18:33
  • You can also import sys; sys.path.append('dirname') to add dirname to your python path for importing. Commented Oct 17, 2015 at 18:41
  • Neither of these works. No matter what I do it keeps telling me that the directory does not exist. Commented Oct 17, 2015 at 21:00
  • Ok, I can install individual .py files. But if I want to install all the files in a folder is what I am having a problem with. Commented Oct 17, 2015 at 21:17

2 Answers 2

0

You have an un-matlab-like (matlab-unlike? dis-matlab-like?) option of putting multiple function definitions into the same .py file. Once the file -- say, fundefs.py -- is on your path, possibly through having issued import sys; sys.path.append('path/to/fundefs');, you can import it

  1. through import fundefs, after which you can access the functions therein by fundefs.fun1, fundefs.fun2 etc.
  2. through from fundefs import *, which will throw all the functions into your current namespace. This is generally discouraged (and frowned upon) for larger modules as it will pollute your namespace, but for a few functions of your own this might just be what you're after. See also this very informative answer (and also comments therein).
  3. as a middle ground through import very_long_and_descriptive_module_name as shorthand to access your functions as shorthand.fun1, shorthand.fun2 etc. (in the obvious case if your definitions are in the file very_long_and_descriptive_module_name.py)
answered Oct 17, 2015 at 22:34
Sign up to request clarification or add additional context in comments.

Comments

0

You don't import User. What you want is to import ted. Typically, you would put ted.py in the same folder as your main python file, not in a separate folder.

answered Oct 17, 2015 at 23:19

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.