Modules / Require / Include function
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 |
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: Code:
execfile('graphModule.py') |
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 |
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. |
OK, thank you.
Johannes |
All times are GMT -7. The time now is 06:17 PM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC