Mac OS X /bin/bash: python: command not found in some IDE

馋奶兔 提交于 2019-12-23 12:59:14

问题


When I compiled test.py(a very simple Python file) in Sublime Text or CodeRunner, I got the error:/bin/bash: python: command not found. Then I input python test.py in the Terminal app, it worked. Later I downloaded Pycharm and compiled the file again, it worked too!

So I assume there is some kind of path setting or something else that was not set correctly. I've searched for quite a long time on the internet but no use. Please help or try to give some ideas how to solve the problem.

Here are some details:

  1. I've tried inserting #! /usr/bin/python at the top of test.py file but no use

  2. The output of echo "$PATH" in Terminal is /usr/local/sbin:/Library/Frameworks/Python.framework/Versions/3.5/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/Users/chenyang/Downloads/android-sdk-macosx/platform-tools

  3. I've found several versions of Python in my macbook :2.6, 2.7, 3.2, 3.3, 3.5. Under the folder /System/Library/Frameworks/Python.framework/Versions I found 2.6, 2.7. Under the folder /Library/Frameworks/Python.framework/Versions I found 3.2, 3,3, 3.5.


I've solved the problem myself and post the answer below


回答1:


The Terminal loads a number of files that can modify your PATH variable, including ~/.profile, ~/.bashrc, ~/.bash_profile, etc. These do not get loaded when the Mac OS X system UI is started / when you login to your user profile via the Finder app. Consequently, apps started via Finder do not inherit the PATH and other environment variables set in these files.

Different versions of Mac OS X have different solutions for setting environment variables such that they are loaded by Finder. Older versions of Mac OS X supported a file called ~/.MacOSX/environment.plist that could be used to specify the environment. Newer versions of OS X use the launchctl tool to set environment variables that are seen by apps started with launchctl (which is responsible for starting the system UI and other system services).

In short, use the command:

launchctl setenv <variable-name> <variable-value>

To set this environment variable for the current user. Apps run as the current user will inherit the variables that are specified. So, for example, you could do:

launchctl setenv PATH "$PATH"

... from the Terminal to apply your current PATH value to the system for your account.

See also:

  • How to set the path for finder launched applications - StackExchange
  • launchctl man page - Mac OS X Darwin Reference



回答2:


Thank everyone who helps. I've solved the problem myself.

I've always been using zsh instead of bash. After updating CodeRunner to the newest version, the app uses bash by default. So I just need go to Preference>Advanced menu and untick the checkbox invoke bash in login mode when running code to solve the problem.

In Sublime Text3, the solution is in this link:https://stackoverflow.com/a/38574286/6631854



来源:https://stackoverflow.com/questions/38554566/mac-os-x-bin-bash-python-command-not-found-in-some-ide

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!