简体   繁体   中英

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

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 #! /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

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM