January 26th, 2005, 06:38 PM
Including other scripts
I am looking for a way to be able to include anouther file(well script) into my code. Like in php you can use the include("link/to/file");
I am hoping there is a way to do it in python, i know about updating the verible $PYTHONPATH or just putting the script in the lib dirctory. But i want an app that can move from computer to computer with ease and doesn't have to have the user do that. I would just create an exe but it has to also be able to run on linux. So if there are any thing that would just include the other file. Even if it isn't in an object form and is just like php where you are basicly just putting all of the code that is in that other file into the current script.
January 26th, 2005, 07:26 PM
You're probably looking for the import statement, http://python.org/doc/2.4/ref/import.html
Doesn't work in the same way as php's include/require functions though
For example, to "include" example.py just simply do
January 26th, 2005, 07:33 PM
from distutils.core import myfilename.py
January 26th, 2005, 10:40 PM
like i said, i already know about all of those ways. I am not looking to import a module in the lib folders or anything. I am looking to be able to do it from my scripts current director or a sub director of my choice that does't have to be in the $PYTHONPATH
January 27th, 2005, 03:25 PM
I did not get it but this might help you.
correct me if i am wrong i dont remeber i was to ill to understand.
but you always need a __init__.py in the dir1 and dir2.
January 27th, 2005, 03:42 PM
The simplest solustion is to put the module/package* in the same directory as your program, this way you can import it like you any other module without having to mess around with sys.path or $PYTHONPATH. This means that you can keep your program and modules together, like you wanted .
Edit: This also prevents conflicts with existing modules with the same name (which you don't want to replace), and improves portability across platforms!
*What monkey is describing is called a package. Its just a directory with a __init__.py file in it, used to group related modules in the same place.
Last edited by netytan; January 27th, 2005 at 03:51 PM.
January 27th, 2005, 04:47 PM
wow, lol, i thought i tested that before and it didn't work. Thanks very much. that works like a charm