Execute Python function from Powershell (how to pass arguments)

Thing you want to do

I want to execute a Python function from Powershell

point

--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]

Sample code

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

Where I stumbled

-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

Execute Python function from Powershell (how to pass arguments)
[Python / Tkinter] How to pass arguments to command
Let's understand how to pass arguments (Python version)
[Python] How to call a c function from python (ctypes)
How to access wikipedia from python
How to use python zip function
How to sample from any probability density function in Python
How to pass arguments when invoking python script from blender on the command line
How to pass arguments to a Python script in SPSS Modeler Batch
How to update Google Sheets from Python
[python] How to use __command__, function explanation
How to access RDS from Lambda (python)
When running a Python shell from Electron, pass multiple arguments to run Python.
Automatically register function arguments to argparse in Python
How to open a web browser from python
To execute a Python enumerate function in JavaScript
How to create a function object from a string
Study from Python Hour7: How to use classes
[Python] How to read data from CIFAR-10 and CIFAR-100
How to generate a Python object from JSON
How to handle Linux commands well from Python
[Python] How to use hash function and tuple.
Execute Power Query by passing arguments to Python
How to install Python
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
How to install python
Execute command from Python
Python: About function arguments
Execute command from python
I want to pass an argument to a python function and execute it from PHP on a web server
[Python] How to remove duplicate values from the list
How to scrape image data from flickr with python
[Introduction to Python] How to iterate with the range function?
Specifies the function to execute when the python program ends
Consider a conversion from a Python recursive function to a non-recursive function
How to execute a command using subprocess in Python
How to create a kubernetes pod from python code
[2020.8 latest] How to install Python
Post from Python to Slack
How to install Python [Windows]
Cheating from PHP to Python
[Python] How to use list 1
How to call a function
How to use Python argparse
Python: How to use pydub
[Python] How to use checkio
Switch from python2.7 to python3.6 (centos7)
How to run Notepad ++ Python
Connect to sqlite from python
How to change Python version
How to develop in Python
[python] How to judge scalar
[Python] How to use input ()
How to use Python lambda
[Python] How to use virtualenv
python3: How to use bottle (3)
python3: How to use bottle
How to use Python bytes
How to slice a block multiple array from a multiple array in Python
How to launch AWS Batch from a python client app