1.11M Members

Running Python Script from Within Another Python Script

 
0
 

I don't know if anyone here is familiar with the Scala digital signage programs but hopefully this question can be answered without any knowledge of it, just in case...

I am working with a Python script that checks a csv file and shares some of the info with a scala script. However, I am now adding some things to the script so I need it to run an additional python script should the appropriate 'if' branch be run.

if (roomname.lower() == svars.banquet_room.lower()) and \
		is_current(evtdate, timeon, timeoff):
                svars.display_evt = True
                svars.evtdate = evtdate
                svars.banquet_room = roomname.title()
                svars.evtname1 = evtname1
                svars.evtname2 = evtname2
                svars.evtname3 = evtname3
                svars.evttime = evttime
                svars.bkgrd00 = bkgrd00
                svars.bkgrd0 = bkgrd0
                svars.bkgrd1 = bkgrd1
                svars.bkgrd2 = bkgrd2
                svars.bkgrd3 = bkgrd3
                svars.bkgrd4 = bkgrd4
                svars.flash0 = flash0
                svars.flash1 = flash1
                svars.flash2 = flash2
                svars.flash3 = flash3
                svars.flash4 = flash4
                os.system('python time_keeper.py')
    
                # figure out font sizes for all three evtnames
                for j in [1,2,3]:
                    evtname = getattr(svars, 'evtname%s' % j)
                    fontsize = 'fontsize%s' % j
                    if   len(evtname) <= svars.short_len:  
                        setattr(svars, fontsize,  svars.largefont)
            	    elif len(evtname) >= svars.long_len:  
                        setattr(svars, fontsize, svars.smallfont)

                break # once found
            else:
                svars.display_evt = False

That is the section of code dealing with running the additional script. Line 21 is how to go about running the script from what I've gathered other places online but it does not seem to work properly. Am I doing something wrong or is this problem located elsewhere?

 
0
 

You don't say what 'does not seem to work properly' means. It may be as simple as: Your $PATH and the program's are different. Try spelling the abspath to python in line 21 and see if that helps. If the script needs to live in a variety of places, you may want something like

robust_heuristic_to_find_python_path():
  for d in listOfObviousAndNonObviousLocations:
     if pythonIsThere(d):
       return thePythonAt(d)
  raise Exception("Can't find Python!")

Where I'm being deliberately obtuse about how you actually spell the python interpreter (python.exe?) sys.path may have something useful, though maybe not. As might os.system("whereis python") on *nix systems. Does Windows have a 'which' command? Maybe?

 
0
 

You don't say what 'does not seem to work properly' means.

I can't exactly say what it means because the python file I am running it from does not do anything immediately visible on the computer it is being run on. It searches a csv file for variables and then passes the variables on to another program. It still does that perfectly but it does not start the other python file up, the way I had intended it to

 
1
 

If you own the script, then you can instrument it: print("I am here") for a variety of 'here' around the interesting places in the script. You can also instrument the called script to do the same thing. You can also experiment with two very simple scripts: testouter.py

import os
print("outer above")
os.system('python inner.py')
print("outer below")

and testinner.py

print("I am inner")

Now, poke at outer.py line 3.

  • Does it need to be os.system('/usr/bin/python inner.py') ? (your own path to python, of course)
  • Does it need to be os.system('python ./inner.py') ?
  • Does it need both?
  • Once that is working, move inner.py to a different directory and make it work again.
  • Do you need an os.path.abspath() around the inner.py path?
 
0
 

If you own the script, then you can instrument it: print("I am here") for a variety of 'here' around the interesting places in the script. You can also instrument the called script to do the same thing. You can also experiment with two very simple scripts: testouter.py Now, poke at outer.py line 3.

  • Does it need to be os.system('/usr/bin/python inner.py') ? (your own path to python, of course)
  • Does it need to be os.system('python ./inner.py') ?
  • Does it need both?
  • Once that is working, move inner.py to a different directory and make it work again.
  • Do you need an os.path.abspath() around the inner.py path?

I appreciate the suggestions. Playing around with things always helps in understanding how they work, I just have to know what to play around with.
That being said, I am playing around with the simple testouter.py and inner.py files you suggested and I got it to work fine using a simple os.system('python inner.py') when they were in the same folder. However, once I moved inner.py to a different folder, I could no longer get things to work, even with the os.path.abspath() command around the inner.py location.
Perhaps the problem lies in my understanding of file paths in Python in a Windows environment but I see no problem with typing os.system('python os.path.abspath(C:/My Documents/Folder/Subfolder/inner.py)') for example.

 
0
 

jsut provide the file path without the os.path.adspath call.
;)

 
0
 

jsut provide the file path without the os.path.adspath call.
;)

this gives me a can't open file 'C:/Documents' error (I'm assuming that is because of the space after Documents in the file path but I don't know how else to indicate that

 
0
 

use doubble slashes

c:\\My Document\\"
 
0
 

Actually, os.path.join() should solve all ills: Pass it *[list-of-all-elements-in-the-path]. For instance os.path.join('C:\\my documents','test cases',python-tests'). I don't have a Windows box to try such things on. See also normpath and realpath .

When I suggested using abspath I was thinking you would do something like os.path.abspath("../../folder2/inner.py") or similar.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: