Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
SlickEdit: Code in over 40 languages across 7 platforms. SlickEdit’s unmatched power, speed, and flexibility allows even the most accomplished developers to write better code faster. Download a free trial today!
  #1  
Old August 16th, 2003, 11:35 AM
aldox aldox is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 4 aldox User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Include files

Is there any way to include another python files in the main program, for example i've two files in python and i want to share the functions between them.

Reply With Quote
  #2  
Old August 16th, 2003, 12:03 PM
percivall percivall is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 133 percivall User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
You use the import statement, like with any module. The code will be imported into its own namespace, and any top-level code will be executed.

To import into the same namespace, use the 'from your_module import your_things' construct.

Reply With Quote
  #3  
Old August 16th, 2003, 12:11 PM
aldox aldox is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 4 aldox User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy Does not work

Seems that this does not work, i''ve

from pfornumb.py import *

if __name__ == '__main__':
print "Hola mundo"


and I get this

Traceback (most recent call last):
File "pruimp.py", line 4, in ?
from pfornumb.py import *
ImportError: No module named pfornumb.py

Reply With Quote
  #4  
Old August 16th, 2003, 03:22 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 18 m 50 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
The error your getting means that Python can't find the file you want to import.. In order for you to import a module into Python it must be on Python's search path (sys.path).

If the pfornumb.py isn't in the same location as your main program you will need to move it. The import statment doesn't use the files extension i.e .py so it should look like this .

from pfornumb import *

alternatively it could look like this

import pfornumb

After Python imports a module for the first time (of if the files changed) it compiles it to Python bytecode (.pyc), this is more a speed thing than anything else. Don't be comfused if you suddenly knowtice a .pyc file a long with your .py files.

To check what's your your search path you can do somthing like this .

>>> import sys
>>> sys.path
['C:\\PYTHON23\\Lib\\idlelib', 'C:\\WINDOWS\\SYSTEM\\PYTHON23.zip', 'C:\\Python23', 'C:\\PYTHON23\\DLLs', 'C:\\PYTHON23\\lib', 'C:\\PYTHON23\\lib\\plat-win', 'C:\\PYTHON23\\lib\\lib-tk', 'C:\\PYTHON23\\lib\\site-packages']

I hope this helps ,

Have fun,
Mark.

Last edited by netytan : August 16th, 2003 at 05:26 PM.

Reply With Quote
  #5  
Old August 20th, 2003, 02:17 AM
TheBlackMamba TheBlackMamba is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2003
Posts: 25 TheBlackMamba User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via AIM to TheBlackMamba Send a message via Yahoo to TheBlackMamba
Actually, your problem is that you had a .py in the import statement.
You don't need the .py.
import q.py #WRONG
import q #RIGHT

Reply With Quote
  #6  
Old August 20th, 2003, 08:23 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 18 m 50 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
Didn't i just say that Mamba .

Quote:
The import statment doesn't use the files extension i.e .py


I just chose to explain what was going on as well as how to fix it

Mark.

Last edited by netytan : August 20th, 2003 at 08:26 AM.

Reply With Quote
  #7  
Old June 22nd, 2008, 11:02 PM
ozbecool ozbecool is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 2 ozbecool User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 16 sec
Reputation Power: 0
And how to ADD a new path to SYS PATH ? I tried :

sys.path += "\\TEMP"

and it did not work - it added each letter one by one to the path (T,E,M,P) instead of adding TEMP

Reply With Quote
  #8  
Old June 23rd, 2008, 03:47 AM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Tampere, Finland
Posts: 90 SuperOscar User rank is Sergeant (500 - 2000 Reputation Level)SuperOscar User rank is Sergeant (500 - 2000 Reputation Level)SuperOscar User rank is Sergeant (500 - 2000 Reputation Level)SuperOscar User rank is Sergeant (500 - 2000 Reputation Level)SuperOscar User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 19 h 47 m 31 sec
Reputation Power: 12
Quote:
Originally Posted by ozbecool
And how to ADD a new path to SYS PATH ? I tried :

sys.path += "\\TEMP"

and it did not work - it added each letter one by one to the path (T,E,M,P) instead of adding TEMP


sys.path is a list, so try:

Python Code:
Original - Python Code
  1. sys.path.append('\\temp')

Reply With Quote
  #9  
Old June 24th, 2008, 02:08 AM
ozbecool ozbecool is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2008
Posts: 2 ozbecool User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 9 m 16 sec
Reputation Power: 0
system path

Thanks - the APPEND worked fine

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Include files


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway