Log in

View Full Version : Modules / Require / Include function


Johannes
04-27-2005, 10:18 AM
Developing different programs that use some of the same code I was wondering if python also has a fuction like e.g. php to include small code snippets at certain positions.

I've already used the module-idea (e.g. import graphModule and then using it with graphModule.blabla ) but it is not the same as just outsourcing part of the code.

Is there some command to do this?

Johannes

farshizzo
04-27-2005, 10:35 AM
Hi,

Python doesn't have a built-in way of doing this. You pretty much have to use the import syntax. You could try the following:execfile('graphModule.py')However I would recommend just using the import syntax.

Johannes
04-27-2005, 10:44 AM
Thank you for the fast answer.

Yes, execfile('graphModule.py') works. Why would you not recommend it?

Is the file called loaded from the harddrive every time I call execFile(File)?
=> slow down

Johannes

farshizzo
04-27-2005, 10:54 AM
Hi,

I believe every time you call execfile, python will look on the hard drive for the filename. I think Python optimizes this a little by compiling the script into a .pyc file, and using that on subsequent calls. I'm not sure if Python stores the code in memory for further optimizations.

There really is no harm in doing this, I just see it as a bad workaround to simply importing the module.

Johannes
04-27-2005, 11:12 AM
OK, thank you.
Johannes