Mac High Sierra终端执行f2py提示‘command not found’求助
Hey there, I’ve run into similar headaches where system updates mess up tool paths, so let’s walk through fixing this f2py issue step by step:
1. First, confirm f2py’s exact location
Before making changes, double-check where f2py is actually stored. Run this command in your terminal:
find /usr/local/lib/python2.7/site-packages/numpy -name "f2py*"
You’ll likely see either f2py (a standalone executable script) or f2py.py (a Python script). Jot down the full path—this will help tailor the fix to your setup.
2. Quick temporary fix (current terminal session only)
If you need to use f2py right away without permanent changes, pick one of these options:
- Add the directory to your PATH temporarily:
After running this, typingexport PATH=$PATH:/usr/local/lib/python2.7/site-packages/numpyf2pyshould work for this terminal window. - Create an alias for f2py:
If f2py shows up asf2py.py, alias it to run via Python directly:
Now typingalias f2py='python /usr/local/lib/python2.7/site-packages/numpy/f2py.py'f2pywill trigger the script automatically.
3. Permanent fix (applies to all future terminal sessions)
To make the fix stick even after closing and reopening the terminal:
- Open your bash configuration file (High Sierra uses
.bash_profileby default):nano ~/.bash_profile - Add one of the following lines at the end of the file:
- If adding the directory to your PATH:
export PATH=$PATH:/usr/local/lib/python2.7/site-packages/numpy - If using the alias approach:
alias f2py='python /usr/local/lib/python2.7/site-packages/numpy/f2py.py'
- If adding the directory to your PATH:
- Save the file by pressing
Ctrl+O, hit Enter, then exit nano withCtrl+X. - Make the changes take effect immediately:
source ~/.bash_profile
4. Bonus check: Verify your Python environment
System updates sometimes reset your default Python version. Run which python to see which Python your terminal is using. If it’s /usr/bin/python (the system default) instead of /usr/local/bin/python (a Homebrew-installed version), that might be the root issue—your old f2py was tied to the non-system Python. Fix this by adding /usr/local/bin to the start of your PATH:
export PATH=/usr/local/bin:$PATH
Add this line to your .bash_profile to make it permanent.
内容的提问来源于stack exchange,提问作者S.K.L.

