I want to execute a Python function from Powershell
--Use command line arguments as arguments --sys.argv is a string, so if you want to pass a number, you need a cast
import sys
#Argument sys.argv[n]Define using
a = sys.argv[1]
calc.py
def add(a, b):
return a + b
main.py
from calc import add
import sys
a = int(sys.argv[1])
b = int(sys.argv[2])
ret = add(a, b)
print(ret)
test.ps
$a = 5
$b = 10
python.exe main.py $a $b
result
6
-Because I passed it to a Powershell function when passing an argument Argument definition error occurred
calc.py
def add(a, b):
return a + b
main.py
from calc import add
ret = add(a, b)
print(ret)
test.ps
$a = 5
$b = 10
python.exe main.py $a $b
result
NameError: name 'a' is not defined
Recommended Posts