473,546 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to execute a python script from another python script?

32 New Member
I have a test environment in python. it is executed by the following command on the command prompt.
1) First i have to go to the dir where the .py file is
2) then i simply execute python filename.py and the GUI appears

Now i have to create a script which could open the GUI...i.e execute the command "python filename.py" from inside itself.. I tried the "system" command inside my script but the GUI did not appear.i.e i tried
system("python filename.py") but nothing appeared on the screen

Plz help.
Mar 21 '07 #1
17 176682
dshimer
136 Recognized Expert New Member
Forgive me if this seems too elementary, but without full code or output to examine I am stretching, did you import os? For example if test.py contains
Expand|Select|Wrap|Line Numbers
  1. import os
  2. os.system('python hello.py')
and hello.py just prints hello, then
Expand|Select|Wrap|Line Numbers
  1. C:\tmp>python test.py
  2. hello
should do what you are asking unless there are other path or module issues. When you say the GUI doesn't appear, does anything happen? Are there errors?
The python "Process Management" docs may be of help.
Mar 21 '07 #2
ghostdog74
511 Recognized Expert Contributor
Forgive me if this seems too elementary, but without full code or output to examine I am stretching, did you import os? For example if test.py contains
Expand|Select|Wrap|Line Numbers
  1. import os
  2. os.system('python hello.py')
and hello.py just prints hello, then
Expand|Select|Wrap|Line Numbers
  1. C:\tmp>python test.py
  2. hello
should do what you are asking unless there are other path or module issues. When you say the GUI doesn't appear, does anything happen? Are there errors?
The python "Process Management" docs may be of help.
to run another python script from your script:
Expand|Select|Wrap|Line Numbers
  1. import myscript
  2. myscript.somefunc()
  3.  
just like importing the standard modules.
Mar 21 '07 #3
somialis
32 New Member
Forgive me if this seems too elementary, but without full code or output to examine I am stretching, did you import os? For example if test.py contains
Expand|Select|Wrap|Line Numbers
  1. import os
  2. os.system('python hello.py')
and hello.py just prints hello, then
Expand|Select|Wrap|Line Numbers
  1. C:\tmp>python test.py
  2. hello
should do what you are asking unless there are other path or module issues. When you say the GUI doesn't appear, does anything happen? Are there errors?
The python "Process Management" docs may be of help.


Thanx for the prompt reply...but as u have asked...i had already imported os and executedthe following command from my python script:
os.system("pyth on filename.py")

no error message was flagged and the command was successfully executed leading to the command prompt again but the GUI did not appear.
What could be the probable reason.??
Initially i used to run the GUI by simply typing:
python filename.py
Mar 22 '07 #4
bartonc
6,596 Recognized Expert Expert
Thanx for the prompt reply...but as u have asked...i had already imported os and executedthe following command from my python script:
os.system("pyth on filename.py")

no error message was flagged and the command was successfully executed leading to the command prompt again but the GUI did not appear.
What could be the probable reason.??
Initially i used to run the GUI by simply typing:
python filename.py
Is your GUI Tkinter, or some other toolkit?
Mar 22 '07 #5
somialis
32 New Member
Is your GUI Tkinter, or some other toolkit?
i don't know about the tool kit...
it was made for some testing puposes. i load my test files into that and press a runtest button.
Mar 22 '07 #6
bartonc
6,596 Recognized Expert Expert
I have a test environment in python. it is executed by the following command on the command prompt.
1) First i have to go to the dir where the .py file is
2) then i simply execute python filename.py and the GUI appears

Now i have to create a script which could open the GUI...i.e execute the command "python filename.py" from inside itself.. I tried the "system" command inside my script but the GUI did not appear.i.e i tried
system("python filename.py") but nothing appeared on the screen

Plz help.
By any chance, do you give command line arguments when you call python manually? If so, try
Expand|Select|Wrap|Line Numbers
  1. system("python filename.py arg1 arg2") 
Mar 22 '07 #7
somialis
32 New Member
By any chance, do you give command line arguments when you call python manually? If so, try
Expand|Select|Wrap|Line Numbers
  1. system("python filename.py arg1 arg2") 

no there are no command line arguments . i just run python filename.py from the command prompt while running the GUI manually.
Mar 22 '07 #8
ghostdog74
511 Recognized Expert Contributor
no there are no command line arguments . i just run python filename.py from the command prompt while running the GUI manually.
personally, i would not recommend this method of calling a python script from anther. The "cleaner" approach is to import it.
for example , in script called myscriptA.py, you have
Expand|Select|Wrap|Line Numbers
  1. def displayGUI():
  2.      print "I am from myscriptA.py in functoin displayGUI"
  3.      return
  4.  
then in myscriptB.py, you can call your GUI display function like this
Expand|Select|Wrap|Line Numbers
  1. import myscriptA ##or from myscriptA import displayGUI
  2. myscriptA.displayGUI()
  3.  
Mar 22 '07 #9
Thekid
145 New Member
Hi, I'm just trying to follow along and had a question.....is it that your script will run in a dos window but what you want is for it to run on IDLE?

ex:
Expand|Select|Wrap|Line Numbers
  1. >>> for i in range(5):
  2.              print i
  3.  
  4.  
  5. 1
  6. 2
  7. 3
  8. 4
  9. >>>
  10.  
  11.  
so if your saved that script as loop.py:

Expand|Select|Wrap|Line Numbers
  1. for i in range(5):
  2.     print i
  3.  
and then wrote:
Expand|Select|Wrap|Line Numbers
  1. import os,sys
  2. os.system(r"C:\python25\loop.py")
  3.  
then saved it as testrun.py. If you execute testrun.py, it runs the loop in a dos window but you want it to execute and print out to IDLE instead? Is that it, or am I on the wrong track in trying follow?
Mar 22 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

4
1957
by: Amir | last post by:
Can one PHP script execute another PHP script?
0
2131
by: Irmen de Jong | last post by:
QOTW: "Confronting the Martellibot is like flirting with an encyclopedia, I'd rather not do it myself, but I respect those who do, because it produces knowledge." -- Anton Vredegoor "Python and Java are not diametrically opposed religions that must fight to the death in jihad, despite the existence of loonies favoring...
1
3055
by: Steve | last post by:
Hi, I've written a small cgi python script that collects feedback from a webform. I need to pass the values to another python script.. how do I do this? Is this even possible? Thanks Steve
8
3017
by: Paul Cochrane | last post by:
Hi all, I've got an application that I'm writing that autogenerates python code which I then execute with exec(). I know that this is not the best way to run things, and I'm not 100% sure as to what I really should do. I've had a look through Programming Python and the Python Cookbook, which have given me ideas, but nothing has gelled...
8
2291
by: Eric_Dexter | last post by:
I was looking for a simple way to load a simple python program from another python program. I tried os.system(cabel) The file name is cabel.py a csound instrument editor.. The error I am getting is
2
2962
by: KraftDiner | last post by:
I have a class that is defined in a file called MyClass.py How do I use that class in another python script.. import MyClass ? (Does it need to be in a specific location?)
2
6019
by: maya | last post by:
hi, I keep getting this error, "can't execute code from a freed script" when I go to another page (i.e., upon UNLOAD..) this error occurs only in IE, not FF (what a surprise.....;) what is this error, pls.. it's for a site I've been working on for a few weeks now, started getting this error just a few days ago.. (I looked this up, saw...
2
2408
by: Alexandru Mosoi | last post by:
how do I execute another python script under a different process? I want the script to be run using the same interpretoer as the one running current script. I tried using os.execlp but I don't know how to get the name/path of the interpreter.
2
4742
by: gintrado | last post by:
I am very new to Python I'm trying to run a python script from within another python script. I can run the script from a unix command line by typing: nohup python script.py password > /tmp/outfile.log 2>&1 & Or, I can put that command in a shell script and run it by typing: ./script.sh Both work great. However I don't know how to put...
0
7507
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7435
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7698
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7461
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5361
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
1
1922
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.