Tools and workflows

How to run a Python script on Windows or Linux

Run a saved .py file in PowerShell or Bash. Find out whether a failed run came from the Python command, the script path, or an input file.

Open a terminal in the folder containing your script. Run python .\hello.py in Windows PowerShell or python3 ./hello.py in Linux Bash. Replace hello.py with the name of your file. If the project specifies a virtual environment, use that environment's Python so the script can find its installed dependencies.

Check your command with a one-line script

Create a directory you can write to and save this as hello.py. Use a plain-text editor. Some Windows editors append .txt if you save under the default text-document file type, creating a file that displays as hello.py when extensions are hidden.

print("The file ran.")

Open a terminal in that same directory. Run the appropriate command. The expected output is one line saying The file ran. This small test separates the mechanics of launching a file from the behavior of a larger project. If this succeeds, you have a working command for a script that needs no packages, arguments, or input files.

Identify the program that reported the error

If PowerShell says the term python is unrecognized, or Bash says python3: command not found, the shell has not found the interpreter command. Check it with Get-Command python in PowerShell or command -v python3 in Bash. On some Windows installations the py command is available and py .\hello.py is a valid alternative. It may choose a different interpreter, so check its version before using it for a project.

An error like can't open file ... means Python started but could not open the script itself. List the files with Get-ChildItem -Name or ls and compare the spelling, extension, and capitalization. You can pass an absolute path to the script. Quote it if any part has a space.

If Python prints a traceback from your source, read the last line first. With ModuleNotFoundError, identify the interpreter and look up the project's dependency instructions. With FileNotFoundError inside your script, check the path it tried to open. The Python error-reading guide walks through the same distinction with more examples.

Check which interpreter is running

Run python -c "import sys; print(sys.executable); print(sys.version)" or use python3 on Linux. This prints the executable and version from the interpreter itself. It is a better check than assuming an editor, terminal, or launcher shares a setting.

A project virtual environment has its own executable. Invoke .\.venv\Scripts\python.exe .\hello.py on Windows or ./.venv/bin/python ./hello.py on Linux from the project folder. Activation is optional with explicit paths. If an execution policy prevents an activation script from running on a managed device, follow local rules and ask which project setup is supported.

Understand paths and arguments

The path to the script identifies the code to run. The working directory determines how relative data paths resolve. Running python C:\Projects\mytool\hello.py from Downloads does not automatically move the working directory to C:\Projects\mytool. If a project's example expects samples/input.txt, start in the project folder or pass a documented absolute argument.

Arguments come after the script path. An argument containing spaces needs quotes, just like the script path. For example, python .\report.py --input "sample files\march.csv" passes a single input filename. The script must define how it handles --input. Python itself does not know what a report's input means.

Read the final line and the source location together

Suppose hello.py runs, but report.py ends with a traceback that points to line 12, in main and then says FileNotFoundError. The interpreter launched successfully. Read line 12 or the deepest frame that belongs to your own code. If it attempts to read inputs/march.csv, list the contents of inputs from the terminal where the command ran. A verified script path does not verify every path mentioned in the script.

If you see SyntaxError, inspect the reported line and the lines just before it. Python can point at the place where it detected the problem, such as the line after an unclosed parenthesis.

Keep the terminal open when a window closes too fast

Double-clicking a file can briefly open a window and then close it when the script exits. Run the same file from a terminal you opened yourself. The shell remains after the command ends, so the printed result or error stays visible. You usually do not need to change the program to pause on exit. A pause would also affect anyone who tried to run the script as part of an automation.

A script that prints nothing could still have written a file. Read its documented output contract. If it is your code, add a concise confirmation to the useful boundary, such as the absolute path of the report you wrote. Test it with a small, disposable output destination before using real inputs.

Run the script again from a fresh shell

An editor terminal can start with a working directory and virtual environment already selected. A second terminal might start in your home directory. Write the change-directory step and interpreter path needed to reach the same result, then verify that sequence in a fresh shell. This practice pays off when you use a teammate's machine or publish instructions.

Understand when the command is ready to share

Record the shell, interpreter version, starting directory, and a tiny sample input. Include the expected result and document whether that result goes to the terminal or to a file. Ask someone to follow your instructions in a clean checkout. A command that relies on an undisclosed editor setting is harder to reproduce than one whose working directory and interpreter are visible.

If you see >>>, you are in the Python prompt. Enter exit() to return to the terminal shell before typing the command. Keep the first check small enough that you can tell whether any failure came from Python, the shell, or your larger script.

Sources and further reading

Related reading

Browse Tools and workflows · Explore project tutorials

Search PythonProjects.org