[Tutor] Defining & Importing Functions

BELSEY, Dylan dylan.belsey@baesystems.com
Fri, 18 Oct 2002 09:51:41 +0930


	Try setting the PYTHONPATH system variable to the directory where
your files exist.  I think that Danny Yoo talked a bit about this in a
previous thread.
	I'm not sure I understand your problem re defining functions in the
same file.  As I understand it, if you write your function up above your
main code then you can just call the function as is.  For example:

>>> def helper():
	print "yo!"

	
>>> if __name__ == '__main__':
	helper()

	
yo!
>>> 

	This also works when in a file and run from the DOS prompt.  The
main thing is that the function must be defined before the main code.

	HTH,
		Dylan


-----Original Message-----
From: James.Rocks@equant.com [mailto:James.Rocks@equant.com]
Sent: Thursday, 17 October 2002 22:27
To: tutor@python.org
Subject: [Tutor] Defining & Importing Functions


Hi,

Up until recently I kept my programs in the Python (c:\python21) install
directory (which somebody told me was not a good idea) ... that in mind I
created a sub-folder for them and put them in there but that caused
problems in that I didn't seem to be able to import from a specified
location i.e. all files imported appeared to need to be in the main python
directory which is (to my mind at any rate) a severe limitation. However,
knowing me, it's probably me & not python :-)

I also have problem running functions that are in the same text file as my
main program ... e.g. if I define a function called "SMPTAlert()" in my
main program how do I "call" it?

My preferred way of organising my files would be to have a main program
file (with all the main routines directly associated with that part of the
program) in one file & any common project functions in a separate project
function file ... these would all be in the project directory. Any routines
that are reusable beyond the scope of the project would go in a separate
project file in a comon folder reachable by any project programs ... in
this case (and I'm unsure whether this is wise or not) that will be the
Python install folder.

Any thoughts?

James

James C. Rocks
Equant
Archway House
Canary Wharf
London E14 9SZ.


_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor