When you write a Python script, you probably run it with a command like python script.py
.
But what if you could run your Python scripts just like any other command-line tool with no prefix, no fuss?
That’s exactly what the shebang line enables.
With one simple line at the top of your script, you can turn it into an executable program that runs directly from the terminal, just like ls
, git
, or make
.
Whether you're building internal tools, automating tasks, or creating command-line utilities, using a shebang is a small but powerful step toward making your scripts cleaner, more portable, and easier to use.
In this article, you’ll learn what a shebang is, how it works with Python, and how to use it to make your scripts executable on any Unix-like system.
What Is a Shebang?
A shebang (also called a hashbang) is a special line at the very top of a script that tells the operating system how to execute the file, specifically, which interpreter to use.
It starts with #!
followed by the full path to the interpreter:
#!/usr/bin/env python3
or
#!/usr/bin/python3
Here’s what’s happening:
- The
#!
sequence (pronounced "shebang") tells the OS: “Use this program to run the file.” - What follows (
/usr/bin/env python3
or/usr/bin/python3
) is the path to the Python interpreter.
This line must be the very first line in your script, no comments or blank lines above it, or the OS will ignore it.
Why Use #!/usr/bin/env python3
?
Using #!/usr/bin/env python3
instead of a hardcoded path makes your script more portable and environment-friendly.
Here’s why it matters:
- The
env
command searches forpython3
in the user's current$PATH
. - This ensures the script runs with the correct interpreter, whether it's the system Python, a user-installed version, or one from a virtual environment.
- It's especially useful when your script might be run on different machines or OS configurations.
Compare the two options:
In short: use `env*`* when you care about portability, especially across macOS, Linux distros, and dev environments.
Making a Python Script Executable
Want to run your Python script directly from the terminal, just like a native command?
Here’s how to do it in three simple steps:
Step 1: Add a Shebang Line
Create a file called hello.py
and start it with the shebang:
#!/usr/bin/env python3
print("Hello, world!")
This tells the operating system to use Python 3 to run your script.
Step 2: Make the Script Executable
Use the chmod
command to give the script execute permissions:
chmod +x hello.py
This step allows the script to be run as a standalone program.
Step 3: Run the Script
Now you can run the script directly from the terminal (no need to prefix it with python
):
./hello.py
You should see:
Hello, world!
That’s it, your Python script is now an executable command!
Run Your Script from Anywhere (Add to $PATH
)
By default, you can only run your script from the directory it lives in.
But if you want to use it like a global command, from any location in your terminal, just move it to a directory that’s included in your system’s $PATH
.
Rename and move your script to a directory in your $PATH
(e.g., /usr/local/bin
or ~/bin
):
mv hello.py /usr/local/bin/hello
💡Note: You might need sudo
to move files into system directories like /usr/local/bin
.
Verify it's executable:
chmod +x /usr/local/bin/hello
Run it from anywhere:
hello
You should see:
Hello, world!
Now your Python script behaves just like any other command-line tool, clean, simple, and accessible globally.
Tip: If you prefer to keep scripts in your home directory (e.g., ~/bin
), make sure ~/bin
is added to your $PATH
. Add this line to your .bashrc
, .zshrc
, or .profile
:
export PATH="$HOME/bin:$PATH"
Then run:
source ~/.bashrc # or source ~/.zshrc
A Note on File Extensions
Once your script has a proper shebang and executable permissions, the .py
extension becomes optional.
For example, instead of naming your script hello.py
, you can simply call it hello
:
mv hello.py /usr/local/bin/hello
Now you can run it just like any other system command:
hello
This is common practice for CLI tools, many system utilities are written in Python but don’t expose their `.py*`* extension, keeping command names clean and professional.
While dropping the extension is fine for production-ready or user-facing scripts, you may want to keep .py
during development to benefit from:
-
Editor support: Syntax highlighting, linting, and type checking work best with
.py
files. -
Tooling compatibility: Test runners, formatters (like
black
orruff
), and debuggers expect.py
files.
As a best practice:
- Use
.py
while developing or sharing source code. - Drop it when installing or deploying the script as a command-line utility.
Use Virtual Environments in Shebangs
If your script relies on third-party packages installed in a virtual environment, you can make sure it always runs with the correct dependencies by pointing the shebang directly to the virtual environment’s Python interpreter:
#!/path/to/your/venv/bin/python
This ensures your script uses the specific Python interpreter, along with all the packages, from your virtual environment, rather than falling back to the system Python.
How to Find the Path
Activate your virtual environment, then run:
which python
You'll get something like:
/home/user/myproject/venv/bin/python
Use this path in your shebang:
#!/home/user/myproject/venv/bin/python
You should use this mainly if:
- You're deploying a script alongside a virtual environment.
- You want strict control over the Python version and dependencies.
- You're bundling a CLI tool for isolated use.
💡Note: Hardcoding virtual environment paths can reduce portability. If the script is meant to be used across machines or by other users, prefer #!/usr/bin/env python3
and activate the virtual environment in the shell instead.
Conclusion
The shebang line is a simple but powerful feature that transforms your Python scripts into first-class command-line tools.
By including it at the top of your file, you can:
- Run scripts directly without typing
python
. - Make your code more portable and easier to share.
- Build clean, user-friendly CLI tools and automation scripts.
Key Takeaways:
- Use
#!/usr/bin/env python3
for maximum portability. - Don’t forget to make your script executable with
chmod +x
. - Move it to a directory in your
$PATH
to run it from anywhere.
With just a few extra steps, you can make your Python scripts behave like native Unix commands, cleaner, faster, and more professional.
Follow me on Twitter: https://twitter.com/DevAsService
Follow me on Instagram: https://www.instagram.com/devasservice/
Follow me on TikTok: https://www.tiktok.com/@devasservice
Follow me on YouTube: https://www.youtube.com/@DevAsService
Top comments (0)