You can run Python in PowerShell the same way as from the command bar.
Run PowerShell Command as Admin
First, you have to start PowerShell as Admin. To do it, just click the magnifying glass on the Windows Toolbar and enter “powershell”.
Next, right-click the icon and run it as administrator.
Run Python code
Now, when you have the PowerShell window open, you can start the Python interpreter by typing “python”.
Run Python Script
In the previous section, you saw a simple text message that was executed in the python interpreter, using PowerShell.
To leave the Python interpreter, press Ctrl + Z and then Enter.
Navigate to the directory where your script is located and use this command:
1 |
python script.py |
It will run the script that is inside the script.py file.
1 |
print('Hello world from the script!') |
You can also run a script without changing the current dictionary, by specifying an absolute path to a file. You can use both forward-slash (/) and backslash (\).
Run Script with Arguments
When you run Excel scripts, you can pass arguments from the command line. Take a look at this code from the sum.py file:
1 2 3 4 5 6 7 8 9 10 |
import sys def sum(a, b): print("Your sum is", a + b) if __name__ == "__main__": a = int(sys.argv[1]) b = int(sys.argv[2]) sum(a, b) |
It takes two numbers from the command line (sys.argv[1] and sys.argv[1]) and sum them returning text and the addition result. sys.argv[0] is the first argument of our entry, which is the name of a script (sum.py).