How to Run a Python File
Run a Python file from a terminal by invoking a Python interpreter with the script’s .py filename after moving to its folder. The command may begin with python, python3, or py, depending on what is installed on your computer. This guide shows the reliable terminal method first, then covers VS Code, virtual environments, modules, arguments, local servers, and common errors.
What Running a Python File Means
A Python script is a .py file containing Python commands, such as app.py or hello.py (University at Albany’s Python Basics guide). Running it means asking a Python interpreter to read the file and execute its code from top to bottom.
Three details decide whether a script runs correctly:
- The Python interpreter command your computer recognizes.
- The folder you run the command from, called the working directory.
- The exact filename, including capitalization and the
.pyextension.
A script can run successfully and still show no output if it does not print anything, write a file, open a window, or start a server. For a first test, use a script that prints a message.
The Reliable Way to Run a Python File From Terminal
Choose the route that matches the project before you begin.
| Situation | Best Route | Why |
|---|---|---|
| A single script using Python’s built-in features | Use the system Python interpreter in a terminal | It is the simplest way to execute one file. |
A project with a .venv folder or installed dependencies |
Use that project’s virtual-environment interpreter | It keeps the project’s packages separate from global Python packages. |
| A package with code intended to be imported | Run it as a module | It preserves package imports and module behavior. |
Step 1: Find or Create the Python File
- Locate the file you want to run and confirm that its name ends in
.py, not.py.txt. - Open a terminal. On macOS and Linux, this is usually Terminal; on Windows, use Command Prompt, PowerShell, or Windows Terminal.
- If you need a test file, create a plain-text file named
hello.pycontaining:
print("Python ran this file")
Avoid double-clicking the file while troubleshooting. A terminal keeps errors visible instead of opening and closing a window too quickly to read.
Step 2: Check Which Python Command Works
Try these commands one at a time. Stop when one prints a Python version.
python --version
python3 --version
py --version
Many macOS and Linux systems use python3. Windows commonly provides the py launcher, while some systems use python.
If none of the commands returns a version, Python is not installed correctly or is not available to the terminal. Install Python for your operating system, close the terminal, open a new one, and test the version command again.
Step 3: Move to the Script’s Folder
Use cd to change directories. Replace the example path with the folder containing your Python file.
On macOS or Linux:
cd /path/to/your/project
On Windows:
cd C:\path\to\your\project
If the path includes spaces, put it in quotation marks. For example:
cd "/path/to/my project"
Running a script from its own folder matters when the script reads relative files such as configuration files, CSVs, templates, or local data.
Step 4: Run the File and Check the Result
Use the Python launcher that worked in Step 2. If your test file is named hello.py, run one of these commands.
python hello.py
python3 hello.py
py hello.py
Replace hello.py with your file’s exact name. A successful test prints:
Python ran this file
The script has finished when the terminal returns to its normal prompt without a traceback. If the script is designed to keep running, such as a server or watcher, stop it with Ctrl+C when you are done.
Run a Python File in VS Code or Another Editor
VS Code and most Python-capable editors can run a script with a button or command, but the integrated terminal is usually the clearest place to start.
Use the VS Code Integrated Terminal
- Open the project folder in VS Code rather than opening only one isolated file.
- Open the integrated terminal.
- Verify the interpreter and run the script from that terminal.
python hello.py
If your system uses another launcher, use that instead.
python3 hello.py
py hello.py
The terminal shows normal output, error messages, command-line arguments, and the script’s exit behavior. It also makes the working directory visible, which helps when a script cannot find a local file.
Use the Run Python File Command
VS Code’s Python tools include a command named Run Python File that starts the open script in a terminal. Make sure VS Code is using the intended Python interpreter for the project before running it.
VS Code’s Run Python File command can add startup delay even when the same script starts immediately in the integrated terminal; testing the integrated-terminal command distinguishes editor launch overhead from slow script startup.
Other IDEs work similarly: select the project interpreter, set the script as the run target, and inspect the editor’s terminal or run-output panel for errors.
Run Projects, Modules, Arguments, and Local Servers
A basic script can use the system interpreter, but most real projects need a more specific execution method.
Run a Project in a Virtual Environment
A virtual environment gives one project its own installed packages. Create one in the project folder if the project does not already have one:
python -m venv .venv
If your system uses a different working launcher, substitute it when creating the environment.
If the project includes a requirements.txt file, install its dependencies with the virtual environment’s interpreter. On macOS or Linux:
.venv/bin/python -m pip install -r requirements.txt
On Windows:
.\.venv\Scripts\python.exe -m pip install -r requirements.txt
Then run the script with that same interpreter. On macOS or Linux:
.venv/bin/python app.py
On Windows:
.\.venv\Scripts\python.exe app.py
I use a Python virtual environment here because it keeps a project’s packages separate from the system interpreter.
Run a Package as a Module
Some projects are intended to run as modules rather than as standalone files. From the project’s parent folder, use module mode:
python -m package.module
For example, if a project contains a package folder named myapp and a file named main.py, the module name is typically myapp.main.
Module mode is especially useful when a file imports nearby project code. It lets Python resolve imports from the package structure instead of treating the file as an isolated script.
Pass Arguments to a Python Script
You can pass values after the script name:
python filename.py arg1 arg2
Python exposes those values through sys.argv. For example, sys.argv[0] is the script name, while later entries hold the arguments supplied in the terminal.
Use quoted arguments when a single value contains spaces:
python filename.py "input file.csv"
Run a Simple Python Server
To serve the current folder through Python’s built-in HTTP server, run:
python -m http.server
The terminal prints a local address you can open in a browser. Keep the terminal open while the server is running, then press Ctrl+C to stop it.
This is useful for previewing static HTML, CSS, JavaScript, or local files during development. It is not a replacement for an application server in a production deployment.
Fix Common Problems Running Python Files
Python Is Not Recognized
Errors such as “command not found” or “is not recognized” mean the terminal cannot find that Python launcher.
Test the available launchers again:
python --version
python3 --version
py --version
If none works, install Python and open a new terminal window afterward. If one works but another does not, use the launcher that returned a version consistently for running scripts and installing project packages.
Python Cannot Find the File
An error stating that Python cannot open a file usually means the terminal is in the wrong folder or the filename does not match exactly.
List the current folder’s files on macOS or Linux:
ls
List them on Windows:
dir
Check the filename, extension, capitalization, and working directory. Then move to the correct folder and run the script by its exact filename.
The Script Cannot Import a Package
A ModuleNotFoundError usually means the script is using a different interpreter from the one where its dependencies were installed.
If the project provides a dependency file, install it with the same launcher used to run the script:
python -m pip install -r requirements.txt
For projects with a virtual environment, run both the dependency installation and the script through the .venv interpreter. This prevents global packages from masking missing project dependencies.
The Script Runs but Shows Nothing
A script can finish immediately without displaying anything. Add a temporary print statement near the top of the file:
print("Script started")
If that message appears, Python is executing the file. The issue is likely in later logic, conditional branches, input handling, or code that is waiting for a resource.
Confirm the Script Runs Reliably
The interpreter choice and working directory are part of how a Python file runs, not incidental terminal details.
Before treating a script as ready, check that:
- The terminal shows the expected output or behavior.
- The command finishes without a traceback unless the script is intended to remain running.
- The interpreter is the one intended for the project.
- The script starts in the folder containing the files it expects to read.
- Dependencies are installed in the same Python environment that runs the script.
- The command is written in the project’s README or notes so another developer can repeat it.
Once the command works, use that same interpreter and project folder when running the script in the future.
Frequently Asked Questions
How Do I Get a .py File?
Create a plain-text file and save it with a .py extension, such as hello.py. Put valid Python code inside it, such as a print() statement, then run it from a terminal with a Python interpreter.
How Do I Run a File Via the Command Line?
Move the terminal into the folder containing the script, then invoke Python with the filename. On macOS or Linux, the folder change can look like this:
cd /path/to/project
Then run the file with a working Python launcher:
python filename.py
If python is unavailable but python3 or py reports a version, use that launcher with the same filename.
Can I Run a Python File With Arguments?
Yes. Put the arguments after the script name in the terminal command.
python filename.py arg1 arg2
Inside the script, Python makes those values available through the sys.argv list. Quote an argument when it contains spaces so the shell treats it as one value.
How Can I Run Python Code in VS Code?
Open the project folder in VS Code, open its integrated terminal, and run the script using the selected project interpreter. For example:
python filename.py
You can also use VS Code’s Run Python File command, but the integrated terminal is better for checking the exact command, current folder, output, and errors.
How Do I Run a Python Server in Terminal?
Run Python’s built-in HTTP server from the folder you want to serve.
python -m http.server
The terminal displays a local address for your browser and continues running until you stop it with Ctrl+C.
How Do I Run a System Command From Python?
Use Python’s subprocess module to start another program and capture failures. This example runs a Git command and raises an error if it fails:
import subprocess
subprocess.run(["git", "status"], check=True)
Pass the command and its arguments as a list rather than building one shell string, which avoids quoting problems and makes argument handling more predictable.
How can you make a Python script executable on Linux?
On Linux, you can make the Python file directly executable by putting #!/usr/bin/env python on its first line and granting it execute permission with chmod u+x name_of_script.py. The env-based shebang lets the system locate the Python installation when it starts the script.
