Як запустити скрипт Python у Windows, macOS і Linux

How to Run a Python Script on Windows, macOS, and Linux

How to run a Python script is one of the first things you need to know when working with tutorials, automation, or small utilities. The simplest method depends on whether Python is already installed, which operating system you use, and whether the script needs extra libraries.

The simplest way to run a Python script

The most reliable option in most cases is to run the script from a terminal or command prompt with python or python3 followed by the file name.

  • Open Terminal, PowerShell, or Command Prompt.
  • Go to the folder that contains the script.
  • Run python script.py or python3 script.py.

The check is straightforward: if the script starts correctly, you will see its output or a new prompt without an error. If the system says the command is not found, Python is either not installed or not added to PATH.

How to run a Python script on Windows

How to run a Python script on Windows is usually easiest through Command Prompt or PowerShell, and double-clicking the file can work for very simple tests.

Run it from the command line

On Windows, the command py script.py often works because it uses the Python Launcher. If that is not available, use python script.py.

  • Open the folder that contains the script.
  • Type cmd in the address bar, or open PowerShell.
  • Run the file with the appropriate command.

If the script does not start, check whether Python responds to python –version or py –version. If no version appears, Python usually needs to be reinstalled with the option to add it to PATH enabled.

Run it by double-clicking

Double-clicking is only practical for simple scripts that do not need keyboard input. The window may close immediately after the script finishes, so it helps to add a pause at the end or run the file from a terminal instead.

How to run a Python script on macOS and Linux

How to run a Python script on macOS and Linux is usually handled in Terminal, where python3 script.py is the standard command on modern systems.

  • Open Terminal.
  • Move to the script folder with cd.
  • Run the file with python3 script.py.

On macOS, the older python command may point to a system version or behave differently than expected, so python3 is the safer choice. On Linux, it is also worth checking the installed version with python3 –version.

What to do if the script does not run

How to run a Python script without errors often comes down to a few checks: the right command, the right folder, the right Python version, and the required dependencies.

  • Command not found. Check that Python is installed and that PATH is set correctly.
  • File not found. Make sure you are in the same folder as the .py file.
  • Module error. Install the missing libraries with pip or activate the virtual environment.
  • The script opens and closes immediately. Run it from a terminal so you can see the error message.

A safe first step is to test a simple file with one line, such as printing text. If that works, the problem is not Python itself but the script or its dependencies.

When to use a virtual environment

How to run a Python script inside a virtual environment matters when a project uses third-party packages and you do not want to mix them with system libraries.

  • Create the environment with python -m venv venv or python3 -m venv venv.
  • Activate it before running the script.
  • Install dependencies with pip inside that environment.
  • Run the file with the usual python script.py command.

The success check is simple: after activation, python –version should show the expected version, and pip list should show the installed packages. That confirms the script is running in the correct environment.

For most users, the best workflow is the same: open a terminal, go to the script folder, check the Python version, and run the file with python or python3. This approach depends least on system settings and is the easiest way to diagnose errors.