|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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.
|
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
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 |
|
#4
|
||||
|
||||
|
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. |
|
#5
|
|||
|
|||
|
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 |
|
#6
|
||||
|
||||
|
Didn't i just say that Mamba
.Quote:
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. |
|
#7
|
|||
|
|||
|
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 |
|
#8
|
|||
|
|||
|
|
|
#9
|
|||
|
|||
|
system path
Thanks - the APPEND worked fine
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Include files |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|