How to call a script from another?

C

Christian

From a not even newbie:

Without knowing much about Python (yet) I'm trying to install the CMS
Zope via FTP (with the well documented changes to make it work on an
Apache server).
By birth Zope is started from a shell script. And not having the
permissions to execute such ones I'll try writing a .py script (with the
shebang that I allready knows will do the job) to call another .py
script like the original shell script does.

So my question is:
How do I call a .py script from another .py script?
(Writing the rest - I hope - is piece of cake ;-) ).

(And yes, I know that there are a lot of other problems but for starters
I just would like to try to call a .py script from another .py script.)


Thanks

Chris
 
J

Jaime Wyant

I think this is the simplest way to do that:

import sys
sys.path.append('/path/to/directory/containg/script')

import zopescript
zopescript.main()

The code above assumes:
o that the path you use to append contains an __init__.py in it...
o zopescript is the module you want to `run'
o main is the method in zopescript that cranks up zope.

I'm not sure of how to do it the hard way...

jw
 
S

Scott David Daniels

Jaime said:
I think this is the simplest way to do that:

import sys
sys.path.append('/path/to/directory/containing/script')

import zopescript
zopescript.main()

The code above assumes:
o that the path you use to append contains an __init__.py in it...
I don't think this is necessary. The __init__.py is necessary
if you do something like:
import sys
sys.path.append('/path/to/directory/containing')
from script import zopescript
zopescript.main()
# or import zopescript.script; script.zopescript.main()
That is, if the directory ".../containing" needs to be interpreted as
a package.

If the script you want to call is in the same directory as the script
calling it (or any directory on the path), you can simply use:
import zopescript
zopescript.main()

--Scott David Daniels
(e-mail address removed)
 
C

Christian

Thanks guy's, you have opened my eyes and made my next step a whole lot
easier.


Chris
 
D

Dennis Lee Bieber

By birth Zope is started from a shell script. And not having the
permissions to execute such ones I'll try writing a .py script (with the
shebang that I allready knows will do the job) to call another .py
script like the original shell script does.
Pardon? If you can't execute "shell scripts", how do you intend to
execute the python script to execute the other python script?
So my question is:
How do I call a .py script from another .py script?
(Writing the rest - I hope - is piece of cake ;-) ).
Well... an overly simplified explanation... You create a command
shell and pass the name of the script to it (back to problem #1)

import os
os.system("python myOtherPython.py")

{If you have a new enough Python release, there is a subprocess module
that has a bit more capability}
(And yes, I know that there are a lot of other problems but for starters
I just would like to try to call a .py script from another .py script.)
OTOH, if the script is designed as an importable module, you could

import myOtherPython
myOtherPython.mOP_main_entry_point()

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top