By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,533 Members | 1,334 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,533 IT Pros & Developers. It's quick & easy.

How to run another .py file w/o exec command?

P: n/a
Hi everybody,

I'm sure there's a way to do this, but I can't find it. How can I execute
another .py file from my first .py file without using an exec* command?
They're both in the same directory, and it would be nice to have some
run("another.py") type statement as opposed to a big exec with absolute
pathnames and garbage like that.

It works the way I have it, but it just seems like a bad way in general to
do it. I'm runnning Python 2.3.3 on win32.

Thanks!
CaptainN

Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Ca******@altel.net wrote:
Hi everybody,

I'm sure there's a way to do this, but I can't find it. How can I execute
another .py file from my first .py file without using an exec* command?
They're both in the same directory, and it would be nice to have some
run("another.py") type statement as opposed to a big exec with absolute
pathnames and garbage like that.

It works the way I have it, but it just seems like a bad way in general to
do it. I'm runnning Python 2.3.3 on win32.

Thanks!
CaptainN

I'm really new to python, but there are several ways with the import
statement:
1) If the code you want to execute is not in a class but just like this:
File: other.py
class A:
pass

print "You've called me!"

then you can just import it with "import other" and the print statement
gets executed, like any other thing at column 0
2) If 1) is not the case i.e.
File: other.py
class A:
pass

def method():
print "You've called me!"

you can import it with "import other", too and just do "other.method()"
or "from other import *" and "method()"

Christian von Essen
Jul 18 '05 #2

P: n/a
Hello CaptainN,
I'm sure there's a way to do this, but I can't find it. How can I execute
another .py file from my first .py file without using an exec* command?
They're both in the same directory, and it would be nice to have some
run("another.py") type statement as opposed to a big exec with absolute
pathnames and garbage like that.

http://www.python.org/doc/current/li...built-in-funcs
Search for execfile

HTH.
Miki
Jul 18 '05 #3

P: n/a
you can try this

import os
os.system("python example.py")
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.