Pycharm does not inherit your PATH on Mac

Getting pre-commit hooks (https://pre-commit.com/) working on Pycharm was more trouble than I had expected.

10:19:00.320: [aws-global-tofu] git -c core.quotepath=false -c log.showSignature=false commit -F /private/var/folders/yn/45720mbn22nf5yc2mzz1lfy00000gn/T/git-commit-msg-.txt --
OpenTofu fmt.............................................................Failed
- hook id: tofu_fmt
- exit code: 127
/Users/emilyzall/.cache/pre-commit/repoikdmwqn_/hooks/tofu_fmt.sh: line 47: tofu: command not found

Eventually I realized it is because when you launch Pycharm on a Mac it doesn’t inherit your PATH unless you launch it from the terminal. You can see this by making a file to show the PATH and then right-clicking the file name and choosing Run then compare that to echo $PATH from your terminal.

# show_path.py
import os
print(os.environ.get("PATH"))
/usr/bin/python3 /Users/emilyzall/repos/aws-gov-test/show_path.py 
/usr/bin:/bin:/usr/sbin:/sbin

➜ echo $PATH
<<way more stuff>>

So I launched it from the terminal with open -a "PyCharm"and it inherits my path and all worked fine.

There is probably a way to make this work from the Mac launcher but my brief research didn’t find way to set the PATH from Pycharm and it doesn’t seem to inherit the launchctl path launchctl getenv PATH. I also saw a suggestion to “create a LaunchAgent Plist” to solve this but I am not really familiar with this and decided not to mess with this for now.

Leave a comment