如何通过命令行运行Python脚本?实现Git式终端全局调用方法
Hey there! Let's break this down step by step—this is a super common confusion when you're getting deeper into shell environments, and I've been right where you are.
First: Why Git works anywhere in the terminal
Git doesn't run "C scripts"—it runs compiled binary executable files (built from C source code). The core reason you can call git from any directory boils down to one key system setting:
- When you install Git, it places its main executable (and helper tools) into a directory that's already in your system's
PATHenvironment variable. Common locations are/usr/binor/usr/local/bin—these directories are included inPATHby default on almost all Unix-like systems (Linux, macOS). - The
PATHvariable tells your shell where to search for executable commands. So when you typegit, your shell checks every directory inPATHin order, finds the Git executable, and runs it—no need to type the full file path.
Your earlier attempt likely had two small missteps:
- You might have added the directory containing your script to
PATH, but forgot to mark the script itself as executable (we'll fix that below). - Typing the
$was a mistake—$is just your shell's prompt, not part of the command you need to run. You only need to enter the command name itself.
How to make your Python script globally callable
Here are two reliable methods to get your Python script working just like git—run it from any directory, just by typing its name:
Method 1: Move the script to a PATH directory
This is the same approach Git uses, and it's the most straightforward:
- Add a shebang line to the top of your Python script:
This tells your shell which interpreter to use to run the script. Add this as the very first line (no spaces before it!):#!/usr/bin/env python3 - Make the script executable:
Run this command in the terminal (replaceyour_script.pywith your actual script name):chmod +x your_script.py - Copy it to a
PATHdirectory:
Pick a directory in yourPATH(you can check yourPATHwithecho $PATH)./usr/local/binis a great choice for user-specific tools. Run:
(You can rename it to a shorter name likesudo cp your_script.py /usr/local/bin/your_scriptyour_scriptso you don't have to type the.pyextension every time.) - Done! Now you can type
your_scriptin any directory to run it.
Method 2: Add your script's directory to PATH
If you don't want to move your script (e.g., you're still editing it often), you can add its current directory to PATH:
- Complete steps 1 and 2 from Method 1 first: Add the shebang line and make the script executable.
- Find your script's full directory path:
For example, if your script lives in/home/your_username/projects/my_scripts, note that exact path. - Edit your shell's configuration file:
- If you use Bash, open
~/.bashrcin a text editor. - If you use Zsh, open
~/.zshrc.
Add this line at the end (replace the path with your script's directory):
export PATH="$PATH:/home/your_username/projects/my_scripts" - If you use Bash, open
- Apply the changes immediately:
Run this command to reload the configuration without restarting your terminal:source ~/.bashrc # Use ~/.zshrc if you're on Zsh - Now you can run your script by typing its name (e.g.,
your_script.pyor justyour_script) from any directory.
内容的提问来源于stack exchange,提问作者LuckyPants

