简体   繁体   English

在某些IDE中找不到Mac OS X / bin / bash:python:命令

[英]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 . 当我在Sublime Text或CodeRunner中编译test.py (一个非常简单的Python文件)时,我收到错误: /bin/bash: python: command not found Then I input python test.py in the Terminal app, it worked. 然后我在终端应用程序中输入python test.py ,它工作正常。 Later I downloaded Pycharm and compiled the file again, it worked too! 后来我下载了Pycharm并再次编译了该文件,它也有效!

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 #! /usr/bin/python at the top of test.py file but no use #! /usr/bin/python位于test.py文件的顶部但没有用

  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 终端中echo "$PATH"的输出是/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. 我在我的macbook中发现了几个版本的Python: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. 在文件夹/System/Library/Frameworks/Python.framework/Versions下,我找到了/System/Library/Frameworks/Python.framework/Versions Under the folder /Library/Frameworks/Python.framework/Versions I found 3.2, 3,3, 3.5. 在文件夹/Library/Frameworks/Python.framework/Versions下,我找到了/Library/Frameworks/Python.framework/Versions


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. 终端加载了许多可以修改PATH变量的文件,包括~/.profile~/.bashrc~/.bash_profile等。当Mac OS X系统UI启动/登录时,这些文件无法加载通过Finder应用程序访问您的用户个人资料。 Consequently, apps started via Finder do not inherit the PATH and other environment variables set in these files. 因此,通过Finder启动的应用程序不会继承这些文件中设置的PATH和其他环境变量。

Different versions of Mac OS X have different solutions for setting environment variables such that they are loaded by Finder. 不同版本的Mac OS X具有不同的解决方案,用于设置环境变量,以便它们由Finder加载。 Older versions of Mac OS X supported a file called ~/.MacOSX/environment.plist that could be used to specify the environment. 较旧版本的Mac OS X支持名为~/.MacOSX/environment.plist的文件,该文件可用于指定环境。 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). 较新版本的OS X使用launchctl工具来设置由launchctl启动的应用程序所看到的环境变量(它负责启动系统UI和其他系统服务)。

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. ...从终端将您当前的PATH值应用于您帐户的系统。

See also: 也可以看看:

Thank everyone who helps. 感谢所有帮助的人。 I've solved the problem myself. 我自己解决了这个问题。

I've always been using zsh instead of bash. 我一直在使用zsh而不是bash。 After updating CodeRunner to the newest version, the app uses bash by default. 将CodeRunner更新到最新版本后,该应用程序默认使用bash。 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. 所以我只需要在运行代码来解决问题时,转到Preference> Advanced菜单并取消勾选在登录模式下调用bash的复选框。

In Sublime Text3, the solution is in this link: https://stackoverflow.com/a/38574286/6631854 在Sublime Text3中,解决方案在以下链接中: https//stackoverflow.com/a/38574286/6631854

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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