Go Back   CodingForums.com > :: Server side development > Other server side languages/ issues > Python

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-30-2011, 06:56 AM   PM User | #1
Apothem
Regular Coder

 
Apothem's Avatar
 
Join Date: Mar 2008
Posts: 380
Thanks: 36
Thanked 25 Times in 25 Posts
Apothem is an unknown quantity at this point
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 09:09 AM..
Apothem is offline   Reply With Quote
Old 01-30-2011, 10:04 AM   PM User | #2
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
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 10:06 AM..
oesxyl is offline   Reply With Quote
Old 01-31-2011, 05:22 AM   PM User | #3
Apothem
Regular Coder

 
Apothem's Avatar
 
Join Date: Mar 2008
Posts: 380
Thanks: 36
Thanked 25 Times in 25 Posts
Apothem is an unknown quantity at this point
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 05:32 AM..
Apothem is offline   Reply With Quote
Old 01-31-2011, 07:45 AM   PM User | #4
oesxyl
Master Coder


 
Join Date: Dec 2007
Posts: 6,682
Thanks: 436
Thanked 890 Times in 879 Posts
oesxyl is a jewel in the roughoesxyl is a jewel in the roughoesxyl is a jewel in the rough
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?

Quote:
Can variables also be in it as well?
yes

Quote:
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
oesxyl is offline   Reply With Quote
Users who have thanked oesxyl for this post:
Apothem (02-01-2011)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:08 AM.


Advertisement
Log in to turn off these ads.