Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts

    How do you "include" a file?

    How do you basically include (or import) a file?
    Such that if I had:

    a.py:
    Code:
    print 'hello'
    b.py:
    Code:
    print 'bye'
    c.py:
    Code:
    <import a.py>
    print "---"
    <import b.py>

    How do I make it so that if I run c.py, it will print out:
    Code:
    hello
    ---
    bye
    Last edited by Apothem; 01-30-2011 at 10:09 AM.

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    a.py:
    Code:
    def dummy1():
        print "Hello"
    b.py:
    Code:
    def dummy2():
        print "world"
    c.py:
    Code:
    if __name__ == '__main__':
       from a import dummy1
       from b import dummy2
       dummy1()
       print '----'
       dummy2()
    running python c.py will do what you want.

    http://docs.python.org/release/2.6.6...l/modules.html

    usualy you need to be sure that import didn't fail and use exception but probably this will be later when you will feel confortable enought with python.

    Edit: two things:
    - change url to point to your python version instead of 2.6.6
    - i assume you don't ask about something like call, eval, exec( see the docs for details)

    best regards
    Last edited by oesxyl; 01-30-2011 at 11:06 AM.

  • #3
    Regular Coder Apothem's Avatar
    Join Date
    Mar 2008
    Posts
    380
    Thanks
    36
    Thanked 25 Times in 25 Posts
    So there's no way to import files and have the imported file automatically evaluate its contents? In other words, if I want to import files I must ONLY have functions/classes in the file(s) to be imported? Can variables also be in it as well?

    Edit: Also, how would you go about importing files from other directories? Suppose you have the following files:
    include/libs.py
    conf/main.py
    test.py

    Lets say you run test.py - how would you import the libs.py and main.py?
    Last edited by Apothem; 01-31-2011 at 06:32 AM.

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Apothem View Post
    So there's no way to import files and have the imported file automatically evaluate its contents? In other words, if I want to import files I must ONLY have functions/classes in the file(s) to be imported?
    no, python doesn't encourage spagetti code like php, because doesn't make sense,
    why do you want to do something like this?

    Can variables also be in it as well?
    yes

    Edit: Also, how would you go about importing files from other directories? Suppose you have the following files:
    include/libs.py
    conf/main.py
    test.py

    Lets say you run test.py - how would you import the libs.py and main.py?
    http://docs.python.org/release/2.6.6....html#packages

    best regards

  • Users who have thanked oesxyl for this post:

    Apothem (02-01-2011)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •