[Tutor] calling user defined function
Alan Gauld
alan.gauld at btinternet.com
Mon Feb 23 01:06:37 CET 2009
"roberto" <roberto03 at gmail.com> wrote
> i can define a function using the text editor provided by IDLE 3.0;
> then i'd like to call this function from the python prompt
>
> but when i try to do it, python warns me that function doesn't exist
> of course if i define the function directly using the >>> prompt,
> after that everything is fine
You need to save the file containing the function in a folder
in the Python search patyh. tHis is defined in sys.path:
import sys
print sys.path
Saving your file in any folder there will allow python to import
the file as a module.
Thus if you save it in
C:\MyProjects\Python\myFunction.py
and C:\MyProjects\Python is in your sys.path
You can then import your file with
>>> import myFunction # notice no .py
And call your function foo() with
>>> myFunction.foo()
You can add folders to sys.path either in a startup script or
using the PYTHONPATH environment variable. Make sure
its PYTHONPATH you create or modify not PATH, they
are very different!
HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list