Mac Shell配置Python命令路径:直接运行pyahp脚本的问题
It’s super annoying having to type those long, clunky paths just to run pyahp—let’s fix that so you can launch it with a simple pyahp command. Here’s what’s going wrong and how to fix it:
Why Your PYTHONPATH Fix Didn’t Work
The PYTHONPATH variable tells Python where to look for modules when you import them in scripts, but it doesn’t help your system find executable command-line tools like pyahp. For that, you need to add the directory containing the pyahp executable to your system’s PATH variable instead.
Step 1: Find Where pyahp is Installed
First, let’s locate the pyahp executable file. Run this command in your terminal:
pip3.6 show pyahp
Look for the line starting with Location—this is where the pyahp package files are stored. The executable script is almost always in a bin directory relative to this location. For your Python 3.6 setup, it’s likely something like:/Library/Frameworks/Python.framework/Versions/3.6/bin/
To confirm the exact path directly, you can run:
find /Library/Frameworks/Python.framework/Versions/3.6 -name "pyahp" -type f
Step 2: Add the Bin Directory to Your PATH
Once you have the bin directory path, add it to your system’s PATH so your terminal knows where to find pyahp.
Temporary Fix (for current terminal session only):
Run this command (replace the path with the one you found):
export PATH="$PATH:/Library/Frameworks/Python.framework/Versions/3.6/bin/"
Now try running pyahp—it should work immediately in this terminal window.
Permanent Fix (works every time you open a terminal):
Add the export line to your shell configuration file:
- If you use Bash: Open
~/.bash_profilein a text editor and paste the export line at the end. - If you use Zsh (default on newer Macs): Open
~/.zshrcinstead.
Save the file, then run source ~/.bash_profile (or source ~/.zshrc) to apply the changes right away.
Alternative: Install pyahp for Your User Account (Easier!)
If you don’t want to mess with system paths, install pyahp in your user’s local directory. This automatically puts the executable in a folder that’s usually already in your PATH.
- Uninstall the existing system-wide installation (if any):
pip3.6 uninstall pyahp
- Install it with the
--userflag:
pip3.6 install --user pyahp
This places the pyahp executable in ~/Library/Python/3.6/bin/, which is almost certainly already in your PATH. Verify by running echo $PATH—look for that directory in the output.
Now you should be able to run pyahp from any terminal window without typing the full path!
内容的提问来源于stack exchange,提问作者William Song

