run python script directly from command line

Learn run python script directly from command line with practical examples, diagrams, and best practices. Covers python, bash, shell development techniques with visual explanations.

Executing Python Scripts Directly from the Command Line

Hero image for run python script directly from command line

Learn how to make your Python scripts executable directly from the command line on Unix-like systems, covering shebangs, permissions, and environment variables.

Running a Python script typically involves invoking the Python interpreter explicitly, like python your_script.py. However, for convenience and to make your scripts behave more like native commands, you can configure them to be executable directly from the command line, simply by typing their name. This article will guide you through the necessary steps, focusing on Unix-like operating systems (Linux, macOS, WSL).

The Shebang Line: Telling the Shell What to Do

The first and most crucial step to making a Python script directly executable is adding a 'shebang' line at the very beginning of the file. The shebang (also known as a hashbang) is a special sequence of characters #! followed by the path to the interpreter that should execute the script. When the shell encounters a shebang, it uses the specified interpreter to run the file, rather than trying to execute it itself.

#!/usr/bin/python3

print("Hello from a directly executable Python script!")
#!/usr/bin/env python3

import sys

if __name__ == "__main__":
    print(f"Hello from {sys.argv[0]}!")
    if len(sys.argv) > 1:
        print(f"Arguments received: {sys.argv[1:]}")

Granting Execute Permissions

Even with the correct shebang, your operating system won't execute the script directly unless it has the necessary permissions. Specifically, the script file needs to have 'execute' permission. This is managed using the chmod command in Unix-like systems.

chmod +x my_portable_script.py

The chmod +x command adds execute permission for the owner, group, and others. If you only want to grant execute permission to the owner, you can use chmod u+x my_portable_script.py.

flowchart TD
    A[Create Python Script] --> B{"Add Shebang Line (e.g., #!/usr/bin/env python3)"};
    B --> C{"Grant Execute Permission (chmod +x script.py)"};
    C --> D{Is script in PATH?};
    D -- Yes --> E[Execute Directly (script.py)];
    D -- No --> F[Execute with Relative/Absolute Path (./script.py or /path/to/script.py)];

Workflow for making a Python script directly executable.

Executing the Script

Once the shebang is in place and execute permissions are granted, you can run your script in a few ways:

1. From the Current Directory

If you are in the same directory as the script, you can execute it by prefixing its name with ./:

./my_portable_script.py

2. Using an Absolute or Relative Path

You can also specify the full path to the script:

/home/user/scripts/my_portable_script.py

Or a relative path from your current working directory:

../scripts/my_portable_script.py

3. From Anywhere (if in PATH)

For the ultimate convenience, you can place your script in a directory that is included in your system's PATH environment variable (e.g., /usr/local/bin, ~/bin). Once it's in a PATH directory, you can simply type its name from any location:

my_portable_script.py

To check your current PATH, use echo $PATH.