#1
  1. ID10T
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Location
    Yes
    Posts
    632
    Rep Power
    27

    call code form another file


    How would i go about calling python code in another file?

    Id liek to seperate my program out a little more, but i would occasionally need to pass information from one file to aother.

    Is this a bad idea?
  2. #2
  3. No Profile Picture
    Javascript is the debbel
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    102
    Rep Power
    12
    well you could either use "import" to import almost anything you need, but since you're looking for more of a way to seperate out what you are making, you might try using execfile().

    execfile() is very similar to include() in PHP, you just give it the file's path and it includes it.
    Last edited by thmnetwork; May 31st, 2005 at 01:09 PM. Reason: Misc. Editing
  4. #3
  5. ID10T
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Location
    Yes
    Posts
    632
    Rep Power
    27
    So i could use import to import functions from any other file? If say i created a file full of functions associated with doing a particular task?
  6. #4
  7. No Profile Picture
    Javascript is the debbel
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    102
    Rep Power
    12
    well I guess it depends on what you're wanting to do. with "import" you get to play with the namespace a little more than with execfile.

    the import method I'm referring to would require you to write an extention package (which isn't really that hard) but allow you to give shorter names to certain classes at import-time.

    using import also uses less characters than execfile() would so I consider it "cleaner looking"

    On the other hand, if you're looking for just some simple way to seperate code out into different files just so you don't have to look at it all all at once, execfile() seems like a more direct solution than writing a whole package for what's just you instantiating classes and such.
  8. #5
  9. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Use execfile().
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Canada
    Posts
    185
    Rep Power
    0
    Originally Posted by Civ247
    So i could use import to import functions from any other file? If say i created a file full of functions associated with doing a particular task?
    if you have a module in say FOO full of functions and you have a program BAR. you can easily call functions from FOO. simply
    Code:
    from FOO import function
    not hard. I do it all the time.
    "In theory, there is no difference between theory and practice.
    But, in practice, there is."

  12. #7
  13. ID10T
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Location
    Yes
    Posts
    632
    Rep Power
    27
    Hmm could i pass in a value using execfile()?
  14. #8
  15. No Profile Picture
    Javascript is the debbel
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2003
    Posts
    102
    Rep Power
    12
    unless you make an effort to change it's environment, it inheirts your own, so you should be able reference any variables you had hitherto created/redefined. it will also be able to update variables for value-checking after the execfile()

    something also occurred to me, you could do it by using system (part of the "os" module) which would "really" execute the script (as if oyu had typed it on the command line). I just thought I'd mention it for completeness, I wouldn't really use it to develop an application with, unless you had a really weird situation.
    Last edited by thmnetwork; May 31st, 2005 at 02:47 PM. Reason: added the bit about system()

IMN logo majestic logo threadwatch logo seochat tools logo