Run Python Script in Windows PowerShell

Muhammad Waiz Khan Feb 23, 2024
  1. Run Python in PowerShell Using py Command
  2. Run Python in PowerShell Using ./<filename>.py
  3. Run Python in PowerShell Using ./<filename>.py in a New Window
  4. Run Python in PowerShell Using python Command
  5. Conclusion
Run Python Script in Windows PowerShell

While Python comes with its own interactive shell, sometimes you may want to run Python scripts or execute Python commands within other environments, such as PowerShell on Windows.

This integration allows you to leverage Python’s capabilities alongside other functionalities provided by PowerShell, enhancing your workflow and productivity.

PowerShell is a command-line shell and scripting language developed by Microsoft for task automation and configuration management.

It provides access to various system functionalities and administrative tasks on Windows.

By running Python scripts in PowerShell, you can combine the strengths of both Python and PowerShell, enabling seamless interaction with Windows system components and resources.

In this tutorial, we will look into the multiple methods to run the Python script or .py files in Windows PowerShell. The requirement to run a Python script on a machine is to have Python installed in it. We can check the installed version of Python 2 or Python 3 in Windows PowerShell with the following commands.

Python 2:

py -2 --version

Python 3:

py -3 --version

If the required version of Python is not installed, it can be downloaded and installed from this link. Once we have the required version of Python, we can run the Python script in Windows PowerShell in the following ways.

Run Python in PowerShell Using py Command

We can run the Python script in PowerShell using the py command. To run the script using py command, we will need to mention the version of Python required to run the script and the name of the script file.

The example command below demonstrates how to run the Python script main.py in Windows PowerShell using py command.

But first, we have to navigate to the directory containing the file you want to open. Use the cd command followed by the path to the directory where your file is located.

If the file is located in a directory on your Desktop called “PythonScripts,” for example, you would type:

cd C:\Users\YourUsername\Desktop\PythonScripts

Here is the code the Python script file:

main.py

def greet(name):
    print(f"Hello, {name}!")


if __name__ == "__main__":
    name = input("Enter your name: ")
    greet(name)
    input("Press Enter to exit...")

This Python script showcases running Python code within the PowerShell environment. It defines a greet() function that takes a name as input and prints a personalized greeting message.

The __name__ == "__main__" block ensures that the greet() function is executed when the script is run directly.

Upon execution, the script prompts the user to enter their name using the input() function and then calls the greet() function with the entered name as an argument.

For Python 2:

py -2 main.py

For Python 3:

py -3 main.py

Output:

run python in powershell output 1

The python script was able to run in PowerShell platform.

Run Python in PowerShell Using ./<filename>.py

We can also execute the Python script through Windows PowerShell using the ./ before the script name.

Normally, It will open a new window and will show the output of the script and close. The problem that can occur in this method is that the user will not be able to see the output of the script, as the output window will close after the execution of the script.

The solution to this problem is to use the input() method at the end of the script. As the input() method waits for the user to enter the input value. Therefore the output window will not close and will remain open until the user does not press the Enter key.

But, it won’t necessarily open a new window by default. The behavior depends on various factors such as the configuration of your system, your default programs, or any specific settings you might have.

Here is the code the Python script file:

main.py

def greet(name):
    print(f"Hello, {name}!")


if __name__ == "__main__":
    name = input("Enter your name: ")
    greet(name)
    input("Press Enter to exit...")

We can use the following command to run the Python script main.py through Windows PowerShell.

py -3 ./main.py

Output:

run python in powershell output 2

In this case, it does not open a new window to execute the Python script.

Run Python in PowerShell Using ./<filename>.py in a New Window

To intentionally run the Python in PowerShell, we can execute it through the code below.

This command explicitly specifies the py command as the process to start and passes the arguments -3 (to use Python 3) and ./main.py (the path to the Python script) using the -ArgumentList parameter.

This way, PowerShell correctly understands the arguments and executes the Python script using the Python 3 interpreter in a new window.

Here is the code the Python script file:

main.py

def greet(name):
    print(f"Hello, {name}!")


if __name__ == "__main__":
    name = input("Enter your name: ")
    greet(name)
    input("Press Enter to exit...")

Command:

Start-Process py -ArgumentList "-3", "./main.py"

When you run Start-Process py -ArgumentList "-3", "./main.py", PowerShell starts a new process using the Python interpreter (py) with the specified arguments (-3 for Python 3 and ./main.py for the path to the Python script), effectively executing the Python script main.py in a separate window.

Output:

run python in powershell output 3

A new window opened and executed the code. When the code is done executing its commands, the window will automatically close.

In this case, it does not close yet due to input() method waiting for the input of the user.

Run Python in PowerShell Using python Command

Another way to run Python scripts in PowerShell is by directly invoking the Python interpreter (python.exe) with the script’s filename as an argument.

This method is straightforward and does not involve any additional commands.

main.py

def greet(name):
    print(f"Hello, {name}!")


if __name__ == "__main__":
    name = input("Enter your name: ")
    greet(name)
    input("Press Enter to exit...")

Here’s how you can do it:

python main.py

When you run python main.py in PowerShell, it will start the Python interpreter and execute the script main.py, displaying any output in the PowerShell console.

Output:

run python in powershell output 4

This method is simple and commonly used for running Python scripts in PowerShell.

Conclusion

The integration of Python scripts into PowerShell environments offers a versatile approach to executing code and automating tasks on Windows systems.

By leveraging Python’s capabilities alongside PowerShell’s administrative functionalities, users can streamline workflows and enhance productivity.

Whether executing scripts directly with py commands, opening scripts in new windows, or invoking Python explicitly with the python command, PowerShell provides flexible options for running Python code seamlessly within the Windows ecosystem.

These methods cater to diverse user preferences and scenarios, making Python-PowerShell integration a valuable tool for developers, administrators, and system users alike.