Skip to content

Effortlessly Run Python Script: A Step-by-Step Tutorial

How to Run Your Python Scripts and Code

A Python script or program is a file containing executable Python code. Being able to run Python scripts and code is probably the most important skill that you need as a Python developer. By running your code, you’ll know if it works as planned. You’ll also be able to test and debug the code to fix errors and issues. Ultimately, you write code in order to run it and accomplish tasks.

In this tutorial, you’ll learn about some of the techniques for running Python scripts and code. The technique that you’ll use in each situation will depend on your environment, platform, needs, and skills.

What Scripts and Modules Are

In computing, the term script refers to a text file containing a logical sequence of orders that you can run to accomplish a specific task. These orders are typically expressed in a scripting language, which is a programming language that allows you to manipulate, customize, and automate tasks.

Scripting languages are usually interpreted at runtime rather than compiled. So, scripts are typically run by some kind of interpreter, which is responsible for executing the code written in the scripting language.

In Python, a script is a file with a .py extension that contains Python code. This code can be a series of statements, functions, classes, or any other code that performs a specific task.

On the other hand, a module is a Python file that contains reusable code such as functions, classes, or variables. Modules are meant to be imported and used by other scripts or modules to extend their functionality.

How to Run Python Scripts From the Command Line

Running Python scripts from the command line is a common and convenient way to execute your code. There are several ways you can run a Python script from the command line:

  1. Using the python command: You can run a Python script by simply navigating to the directory where the script is located and typing python in the command line. This will invoke the Python interpreter and execute the code in the script.

  2. Using the script’s filename directly: If you include the shebang line at the beginning of your script (#! python), you can run the script directly by typing its filename in the command line. Make sure that the script has executable permissions (chmod +x before running it.

  3. Running modules with the -m option: You can run a Python module as a script by using the -m option followed by the module name. This is useful when your module is in a package or you want to execute the module’s __main__ function.

How to Run Python Code Interactively

The Python interpreter provides an interactive mode, also known as the Read-Eval-Print Loop (REPL), which allows you to execute Python code interactively. This is useful for quick tests, experiments, and exploring the Python language.

To enter the interactive mode, open the command line, type python, and press Enter. This will start the Python interpreter, and you can start typing Python code directly. Pressing Enter will execute the code, and you will see the output immediately.

How to Run Scripts From Python Code

Sometimes, you may need to run Python scripts from within other Python code. Python provides several ways to accomplish this:

  1. Taking advantage of import statements: You can import a Python script as a module and then call its functions or access its variables from your main script. This allows you to reuse code and separate functionality into separate files.

  2. Using the importlib standard-library module: The importlib module provides a more flexible and dynamic way to import and run Python scripts. You can use functions like import_module and exec_module to import and execute any Python script programmatically.

  3. Leveraging the power of the built-in exec() function: The exec() function allows you to execute arbitrary Python code stored in a string or compiled code object. This gives you full control over the execution of the script and can be useful in certain situations.

How to Run Python Scripts on IDEs and Code Editors

Most Integrated Development Environments (IDEs) and code editors have built-in support for running Python scripts. They provide features like a dedicated Run button, keyboard shortcuts, and the ability to view the output directly within the editor.

To run a Python script in an IDE or code editor, open the script file, locate the run option or button, and click it. The script will be executed, and the output will be displayed in a designated output window or pane.

Some popular Python IDEs and code editors include PyCharm, Visual Studio Code, and Sublime Text.

How to Run Python Scripts From a File Manager

On some operating systems, you can run Python scripts directly from the file manager. This can be done by associating the .py file extension with the Python interpreter.

To run a Python script from a file manager, navigate to the script file, double-click on it, and the script will be executed by the associated Python interpreter. The output will be shown in a command prompt or terminal window.


Running Python scripts and code is essential for developing and testing your Python applications. In this tutorial, you learned several techniques for running Python scripts, including running from the command line, running interactively with the Python interpreter, running from Python code, running on IDEs and code editors, and running from a file manager.

By mastering these techniques, you’ll have the flexibility and knowledge to run your Python scripts effectively and efficiently.