views:

101

answers:

2

Hi I have a python file that has functions and classes. now I am writting another program (in another file). and I want to start the new file with running the old file (with the function and classes). I have tried using exec(path_2_oldFile.pyw) but it didn't work.

thanks for any help Ariel

+2  A: 

Ideally you should try and import the first file into the new as a module using the import statement:

http://effbot.org/zone/import-confusion.htm

You'll need to make sure that your original module is on the python path somewhere. If it is in the same directory as the new file this should just work.

Andy Hume
Thanks, but I dont want to use it as a module, I want to run it (like F5 does in idle).
ariel
When you import a module, it executes the code inside of it.
Andy Hume
A: 

Importing your file is the prefered method, but if you dont want to import more names to your namespace or you want to run the other file in an absolutely independent mode you can use startfile.

For example:

#file callee.py

print 'hola'
raw_input()


#file caller.py

import os
os.startfile("callee.py")

if you run caller.py it will open a new console were callee.py writes 'hola' and waits for a key press

joaquin