#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25

    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.
  2. #2
  3. No Profile Picture
    root@*.*
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2004
    Location
    London, UK
    Posts
    15
    Rep Power
    0
    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
    Code:
    import example
  4. #3
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Something like,
    from distutils.core import myfilename.py
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    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
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    I did not get it but this might help you.
    import dir0.dir1.dir2.example.py
    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.
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.

    Mark.
    Last edited by netytan; January 27th, 2005 at 03:51 PM.
    programming language development: www.netytan.com Hula

  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2004
    Posts
    461
    Rep Power
    25
    wow, lol, i thought i tested that before and it didn't work. Thanks very much. that works like a charm

IMN logo majestic logo threadwatch logo seochat tools logo