Last modified: Aug 22, 2026

How to Run a Python Script (2024 Guide)

Running a Python script is the first step to executing your code. Whether you’re a beginner or a seasoned developer, knowing the fastest ways to run your code saves time.

You can execute Python files in several ways: using the command line, an integrated development environment (IDE), or by scheduling them automatically. This guide covers the essential methods with practical examples.

We’ll also explore common issues like file paths and permissions. By the end, you’ll run scripts confidently on any operating system.

1. Prerequisites: Install Python

Before running any script, ensure Python is installed on your system. Open your terminal (or Command Prompt) and type:


python --version

If you see a version like Python 3.12.1, you’re ready. If not, download Python from the official website and check “Add Python to PATH” during installation.

On macOS/Linux, you might use python3 instead of python. Use the command that matches your installation.

2. Run a Python Script in the Terminal

The most universal method is using the terminal. First, navigate to your script’s directory using the cd command.

For example, if your script is in ~/projects, type:


cd ~/projects

Then run the script with:


python my_script.py

Replace my_script.py with your actual filename. The script executes immediately, and any output appears in the terminal.

Here’s a simple example script (hello.py):


# hello.py
print("Hello, World!")

Run it:


python hello.py

Output:


Hello, World!

That’s it. This method works on Windows, macOS, and Linux.

3. Run a Python Script with Arguments

Often, you’ll pass data to your script. Use the sys.argv list to capture arguments.

Create a script that accepts a name:


# greet.py
import sys

# The first argument is the script name itself
if len(sys.argv) > 1:
    name = sys.argv[1]
else:
    name = "World"

print(f"Hello, {name}!")

Run it with an argument:


python greet.py Alice

Output:


Hello, Alice!

Without arguments, it defaults to “World”. This is useful for flexible scripts.

4. Run a Python Script in an IDE

IDEs like PyCharm, VS Code, or Thonny offer a graphical way to run scripts. They provide debugging tools and easier project management.

In VS Code, open your .py file and click the play button in the top right corner. The output appears in the integrated terminal.

For PyCharm, right-click the file and select “Run ‘filename’”. This is beginner-friendly because it handles virtual environments automatically.

IDEs also let you set breakpoints and inspect variables. This is ideal for complex projects.

5. Run a Python Script from the Python Interpreter

You can also execute a script from within the Python interactive shell using the exec() function or the runpy module.

Open your terminal and start Python:


python

Then run:


# Inside Python interpreter
exec(open("hello.py").read())

This prints “Hello, World!” without leaving the interpreter. However, this method is less common for production scripts.

For more control, use runpy.run_path("hello.py"). This returns a dictionary of globals, but it’s rarely needed.

Stick to terminal or IDE for daily tasks.

6. Run a Python Script Automatically (Scheduling)

Sometimes you need scripts to run at specific times. Use cron (Linux/macOS) or Task Scheduler (Windows).

On Linux, edit your crontab:


crontab -e

Add a line to run a script every day at 9 AM:


0 9 * * * /usr/bin/python3 /path/to/script.py

On Windows, open Task Scheduler and create a basic task. Point it to your Python executable and script path.

This is perfect for data backups or report generation. Always use absolute paths in scheduled tasks.

7. Common Errors and Fixes

You may encounter a few common issues. Let’s solve them quickly.

“python: command not found” – means Python isn’t in your PATH. Reinstall with the “Add to PATH” option.

“SyntaxError” – check for typos or missing colons. Read the line number in the error message.

“Permission denied” – on Linux/macOS, make the script executable:


chmod +x script.py

Then run with ./script.py if you have a shebang line (#!/usr/bin/env python3).

Also, ensure you’re in the correct directory. Use the pwd command to verify your current path.

8. Best Practices for Running Scripts

Always use virtual environments to manage dependencies. Create one with:


python -m venv myenv

Activate it (Windows: myenv\Scripts\activate, macOS/Linux: source myenv/bin/activate). Then install packages inside it.

This prevents version conflicts between projects. It’s a habit that saves hours of debugging.

Also, use a if __name__ == "__main__": guard in your scripts. This allows importing the script without executing it.


# module.py
def main():
    print("Running directly")

if __name__ == "__main__":
    main()

Now you can import module elsewhere without side effects. This is a professional standard.

For more advanced automation, consider using subprocess to run scripts from other scripts. But for now, master the basics.

Conclusion

Running a Python script is straightforward once you know the tools. Start with the terminal for quick tests, then move to an IDE for larger projects.

Remember to handle arguments, schedule recurring tasks, and use virtual environments. These skills will make you a more efficient Python developer.

Practice with simple scripts first. As you grow, you’ll combine these methods to build powerful applications.

Now, go ahead and run your first script. You have all the knowledge you need.