Reputation: 1
Hi I'm having a function that looks like it is only working in a 2.x (2.7) system. But the rest of my program is written in python 3.4
The file a.py (version 2.7) was a script i could run in 2.7 by calling the script as:
import psspy
openPath='busSystem.raw'
saveToPath='busSystem_out.raw'
#Open a case file
psspy.read(0,openPath)
do some calculation...
#Save to another case file
psspy.rawd_2(0,1,[1,1,1,0,0,0,0],0,saveToPath)
And then calling the following code from python 3.4 in b.py workes
import os
os.system('c:\python27\python a.py')
But then I wanted to change the script in a.py to be a function with kwargs such as:
def run(openPath='busSystem.raw',saveToPath='busSystem_out.raw')
#Open a case file
psspy.read(0,openPath)
do some calculation...
#Save to another case file
psspy.rawd_2(0,1,[1,1,1,0,0,0,0],0,saveToPath)
do something more...
So I want to do something like
import os
in = 'busSystem.raw'
out = 'busSystem_out.raw'
os.system('c:\python27\python a.py run(in, out)')
# Or
os.system('c:\python27\python a.py run(openPath=in,saveToPath=out)')
So the question is:
I know if I could have run the script with python 3.4 i could have just imported the function as
from a import run
run(in,out)
My solution for this would be to read the whole python script as a string, use str.replace('busSystem.raw',in) and str.replace(''busSystem_out.raw',out) and save it back as a a_new.py and run it as mentioned before.
The script in a.py need to be in python version 2.7, because it is interacting with Siemens PSS/E 33, which only communicates through py2.7.
Upvotes: -2
Views: 761
Reputation: 12234
Function calls work only within a single process and, generally, only within a single language. So you have a script that can be run with no arguments. Now you want this script to process command line arguments. This has nothing, really, to do with function calls and keyword arguments.
You should read the Argparse Tutorial in the Python documentation. It introduces the concept of command line arguments, since you seem to be unfamiliar with it, and then shows some examples of using the built-in argparse
module to do the hard parts of parsing the arguments.
Then you should read about the subprocess
module. It will work better for you than os.system()
.
Alternatively, you could update the script so that works correctly in Python 3. This is what I would start with.
Here is some untested example code.
In your existing script a.py
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('openPath')
parser.add_argument('saveToPath')
args = parser.parse_args()
openPath=args.openPath
saveToPath=args.saveToPath
# ... the rest of the existing script
In your other program:
import subprocess
in_ = 'busSystem.raw'
out = 'busSystem_out.raw'
subprocess.call([r'c:\python27\python', in, out])
Upvotes: 1