Home Programming 10 Essential Python Run Command Usages You Need to Know

10 Essential Python Run Command Usages You Need to Know

This article provides a curated list of 10 essential Python run command examples, designed to improve your coding efficiency. From executing scripts to automating tasks, these examples cover a range of scenarios to help you master command-line operations in Python, making your programming workflow more productive.

by Arun Kumar
python run command

In the vast and dynamic world of Python programming, mastering the art of running Python commands in a Ubuntu terminal is a fundamental skill that can greatly enhance your coding efficiency and problem-solving capabilities. From executing simple scripts to leveraging advanced features like profiling and optimizing, understanding how to utilize these commands can significantly impact your development workflow.

In this article, we will explore ten essential Python run command examples, each accompanied by and example, to provide you with a comprehensive toolkit for your Python projects. Let’s get started.

10 Essential Python Run Command Examples You Need to Know

1. Running a Python script

Command: python3 script.py

This is probably the most common way to execute a Python script. By calling python3 followed by the script name, you tell your Ubuntu terminal to interpret the script using Python 3.

Sample Input:

Imagine you have a script named script.py containing the following code:

print("Hello, Python world!")

Sample Output:

Hello, Python world!

Personal Take: There’s something almost magical about seeing your code come to life in the terminal. This command is the bread and butter of Python programming, and while it’s incredibly basic, its simplicity is what makes it so powerful.

2. Executing Python code directly

Command: python3 -c "print('Hello from one-liner!')"

This command is a neat trick for executing a single line of Python code. The -c option tells Python to execute the string that follows as a Python command. It’s incredibly useful for quick tests or demonstrations.

Sample Output:

Hello from one-liner!

Personal Take: I love how this bypasses the need for a script file, making it perfect for those moments when you want to test something quickly. However, it’s not suitable for more complex operations, which is its only downside in my book.

3. Running a Python module as a script

Command: python3 -m http.server

This command starts a simple HTTP server on port 8000 (or another port if specified). The -m option allows you to run a module as a script, and http.server is a module that comes with Python, turning your working directory into a web server.

Sample Output:

Serving HTTP on :: port 8000 (http://[::]:8000/) ...

Personal Take: I find this command incredibly useful for quick web development tests. It’s like having a web server in your back pocket. The downside is that it’s quite basic, but for simple tasks, it’s perfect.

4. Using the interactive Python shell

Command: Just python3 without any script or command.

This opens the Python interactive shell, where you can type and execute Python code directly. It’s an excellent tool for learning, experimenting, or doing quick calculations.

Sample Output:

Python 3.8.10 (default, Jun 2 2021, 10:49:15)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

Personal Take: The interactive shell is like a playground for Python programmers. I love using it to experiment with new libraries or language features. However, it’s not great for developing complex programs, as it lacks the structure and organization of a script.

5. Running Python with warnings

Command: python3 -Wd script.py

This command executes your Python script while also displaying any warnings, such as deprecation warnings, which are usually suppressed. The -Wd option stands for “Warning control” with the d specifying to display all warnings.

Sample Input:

Assume script.py contains deprecated Python features.

Sample Output:

DeprecationWarning: an example warning message

Personal Take: I have a love-hate relationship with this command. While it’s crucial for maintaining up-to-date code, seeing a console full of warnings can be overwhelming. Still, it’s better to be informed than ignorant.

6. Profiling Python scripts

Command: python3 -m cProfile script.py

Profiling is vital for optimizing Python scripts. This command runs your script and outputs a detailed report of execution times for each function, helping you identify bottlenecks.

Sample Output:

         2003 function calls in 0.002 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.002    0.002 script.py:1(<module>)

Personal Take: Profiling is like the diagnostics tool for your Python code. It’s incredibly useful, albeit a bit daunting to interpret at first. Still, learning to read these reports is a game-changer for performance tuning.

7. Checking syntax without execution

Command: python3 -m py_compile script.py

This command compiles the script to bytecode, checking for syntax errors without actually running the script. It’s a quick way to validate your code.

Sample Output:

If there’s an error, it will output something like:

SyntaxError: invalid syntax

Personal Take: It’s a handy tool for quick checks, especially when working on large scripts. I wish I had used it more often in my early days to save time debugging.

8. Running Python in optimized mode

Command: python3 -O script.py

The -O option runs Python in “optimized” mode, which removes assert statements and sets the __debug__ variable to False. This can slightly improve performance.

Sample Input:

A script with assert statements and debugging tasks.

Sample Output:

Outputs of the script, excluding any assert-related code paths.

Personal Take: It’s great for a production run where you want to trim any development or debugging-related overhead. However, use it with caution, as it changes the script’s behavior.

9. Setting environment variables

Command: VAR_NAME="value" python3 script.py

Sometimes, your Python scripts depend on environment variables. This command sets an environment variable for the duration of your script’s execution.

Sample Input:

A script that reads an environment variable named VAR_NAME.

Sample Output:

The output depends on how VAR_NAME is used within script.py.

Personal Take: This approach is a lifesaver for testing scripts that require specific environment settings. It’s straightforward and keeps your system’s environment variables clean.

10. Running a script with a custom Python path

Command: PYTHONPATH=/path/to/module python3 script.py

This command temporarily adds a directory to the Python path, allowing you to import modules from a custom location that is not installed in the standard Python directories.

Sample Input:

A script that imports a module from /path/to/module.

Sample Output:

The script runs as if the module were installed in a standard directory.

Personal Take: It’s an elegant solution for running scripts that depend on modules located outside your usual Python environment. However, for long-term projects, consider properly installing the module or using a virtual environment.


After delving into these ten Python run command examples, it’s clear that Python offers a robust set of tools for developers to effectively manage and execute their code. Whether you’re checking syntax, running modules as scripts, or setting custom environment variables, each command serves a unique purpose in the development lifecycle. By incorporating these commands into your regular programming practice, you’ll not only streamline your coding process but also deepen your understanding of Python’s versatile environment. Happy coding!

You may also like

Leave a Comment



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us


©2016-2023 FOSS LINUX



“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.