在 OSX 上调用 python 和 Spyder 的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20723977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ways to invoke python and Spyder on OSX
提问by kuzzooroo
I recently bought a MacBook and install Python on it via Anaconda. Here's the version information:
我最近买了一台 MacBook 并通过 Anaconda 在其上安装 Python。以下是版本信息:
Python 2.7.6 |Anaconda 1.8.0 (x86_64)| (default, Nov 11 2013, 10:49:09)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
I'd like to be able do all of the following:
我希望能够执行以下所有操作:
- Launch Spyder without having to go through the Launcher application (say, by typing things into Spotlight and Quicksilver)
- Run Python code from the Finder, presumably by double-clicking on my .py file or by selecting and and pressing Cmd-O
- Run Python code by typing a Python script's name into Quicksilver (as if it was a regular program)
- 无需通过 Launcher 应用程序即可启动 Spyder(例如,通过在 Spotlight 和 Quicksilver 中输入内容)
- 从 Finder 运行 Python 代码,大概是通过双击我的 .py 文件或选择并按下 Cmd-O
- 通过在 Quicksilver 中键入 Python 脚本的名称来运行 Python 代码(就像它是常规程序一样)
Here's what I've tried:
这是我尝试过的:
- (nothing; no ideas here)
- I've associated .py files with /Users/kuzzooroo/anaconda/python.app/Contents/MacOS/python and made them executable (chmod u+x). When I try to run the application never launches and I don't get an error message (don't know where to look)
- Made the files executable and tried to run them from Spotlight. Same result as #2.
- (没有;这里没有想法)
- 我已将 .py 文件与 /Users/kuzzooroo/anaconda/python.app/Contents/MacOS/python 相关联,并使它们可执行(chmod u+x)。当我尝试运行该应用程序时,该应用程序永远不会启动,并且我没有收到错误消息(不知道在哪里查看)
- 使文件可执行并尝试从 Spotlight 运行它们。与#2 相同的结果。
Here's what I cando:
这是我可以做的:
- Run my code from a Terminal window, if I have the right shebang on top and have made the file executable
- Generate an application using py2app. Unfortunately, this method suffers the following drawbacks:
- I frequently modify my scripts and would prefer not to have to run py2app every time I do
- Even for a trivial script the resulting app is 7 MB
- To run the application in some contexts I have to dig around in the bundle to find the actual "Unix Executable File" and run that instead
- 从终端窗口运行我的代码,如果我在顶部有正确的shebang并使文件可执行
- 使用 py2app 生成应用程序。不幸的是,这种方法有以下缺点:
- 我经常修改我的脚本,并且不想每次都运行 py2app
- 即使对于一个简单的脚本,生成的应用程序也是 7 MB
- 要在某些上下文中运行应用程序,我必须在包中挖掘以找到实际的“Unix 可执行文件”并运行它
Once I have this working, I'm interested in doing the following:
A. Controlling which scripts create a console window when they run and which do not
B. Controlling whether the resulting console window disappears when the script completes or waits around for the user (me) to close it
However, I suspect it may be easy to figure out how to do these (or whether it's possible) once I've figured out how I'm going to invoke my scripts in the first place.
一旦我开始工作,我有兴趣执行以下操作:
A. 控制哪些脚本在运行时创建控制台窗口,哪些不创建
B. 控制生成的控制台窗口在脚本完成或等待用户时是否消失(me) 关闭它
但是,我怀疑一旦我弄清楚如何首先调用我的脚本,就很容易弄清楚如何执行这些(或是否可能)。
采纳答案by Thomas
To make spyder callable from Spotlight or Finder:
使 spyder 可从 Spotlight 或 Finder 调用:
Locate where your spyder executable is by running in Terminal:
which spyderThis should yield
~/anaconda/bin/spyderif you installed spyder via Anaconda,/opt/local/bin/spyderif you used MacPorts or something similar.Create a file called
spyderin yourApplicationsdirectory and make it executable. Then, fill it with the output of the previous command, followed by a&; exit:touch /Applications/spyder chmod +x /Applications/spyder echo -e '#!/bin/bash'"\n~/anaconda/bin/spyder &\nexit" >> /Applications/spyder(if you use a different shell (e.g.
tcsh), replacebashby that)Under Terminal -> Preferences -> Profiles -> "default profile" -> Shell -> When the shell exits: Select "Close if the shell exited cleanly"
通过在终端中运行来定位您的 spyder 可执行文件所在的位置:
which spyder~/anaconda/bin/spyder如果您通过 Anaconda 安装 spyder,/opt/local/bin/spyder如果您使用 MacPorts 或类似的东西,这应该会产生。创建一个
spyder在您的Applications目录中调用的文件并使其可执行。然后,用上一个命令的输出填充它,后跟一个&; exit:touch /Applications/spyder chmod +x /Applications/spyder echo -e '#!/bin/bash'"\n~/anaconda/bin/spyder &\nexit" >> /Applications/spyder(如果您使用不同的外壳(例如
tcsh),请替换bash为)在终端->首选项->配置文件->“默认配置文件”->外壳->当外壳退出时:选择“如果外壳干净地退出则关闭”
Optional:
可选的:
Download the spyder Icon from hereand open it in Preview. Copy its contents by hitting cmd+C.
In Finder, locate
/Applications/spyderand open its "Get Info" pane by hitting cmd+I. Select the icon in the top left corner with your mouse and hit cmd+V.
从这里下载 spyder 图标并在预览中打开它。按 cmd+C 复制其内容。
在 Finder 中,
/Applications/spyder按 cmd+I找到并打开其“获取信息”窗格。用鼠标选择左上角的图标,然后按 cmd+V。
回答by asmeurer
You can open spyder by pointing Quicksilver to ~/anaconda/bin/spyder(you can add it to your catalog so that it always finds it).
您可以通过将 Quicksilver 指向来打开 spyder ~/anaconda/bin/spyder(您可以将其添加到您的目录中,以便它总能找到它)。
回答by Andy Anderson
Navigate to anaconda/bin, locate spyder (or IPython, etc.), and drag it to the dock — but put it in the documents section at the end. Then you'll have easy access to it, and when you click on it Mac OS X will launch a shell that runs it, and it will appear in the applications section, so that you can bring the running application forward by clicking on it.
导航到 anaconda/bin,找到 spyder(或 IPython 等),然后将其拖到 Dock 中——但将其放在最后的文档部分。然后您就可以轻松访问它,当您单击它时,Mac OS X 将启动一个运行它的外壳,它会出现在应用程序部分,这样您就可以通过单击它使正在运行的应用程序前进。
回答by jncweb
You can use Automator to create an Application that will run Unix Script. Open Automator when it asks for document type put "Application"
您可以使用 Automator 创建一个将运行 Unix 脚本的应用程序。当它要求文档类型时打开 Automator 放入“应用程序”
Search and click on "Run Shell Script". You can leave /bin/bash as it is and type in the box the location of spyder (as given by typing which spyder in the terminal). Then you save the file and you are done. This also gets rid of the problem I had when terminal runs in the background and gives you a application you can drag to the dock. You can also change the logo as suggested above.
搜索并单击“运行 Shell 脚本”。您可以保留 /bin/bash 原样并在框中键入 spyder 的位置(通过在终端中键入 which spyder 给出)。然后你保存文件,你就完成了。这也解决了我在后台运行终端时遇到的问题,并为您提供了一个可以拖动到 Dock 的应用程序。您还可以按照上面的建议更改徽标。
(I did this with Mac OS 10.10 and a Anaconda 2.3.0)
(我用 Mac OS 10.10 和 Anaconda 2.3.0 做到了这一点)
回答by topoman
The accepted answer has two drawbacks: a console windows appears when starting spyder and one cannot keep a spyder icon in the dock. These drawbakcs can be avoided by creating a proper Mac application bundle for spyder, which is surprisingly easy to do.
接受的答案有两个缺点:启动 spyder 时会出现一个控制台窗口,并且不能在 Dock 中保留 spyder 图标。可以通过为 spyder 创建合适的 Mac 应用程序包来避免这些缺点,这非常容易做到。
To convert /usr/local/bin/spyder3(the result of which spyder3on my machine) into a traditional Mac application:
将/usr/local/bin/spyder3(which spyder3在我的机器上的结果)转换为传统的 Mac 应用程序:
Create a Mac application bundle (basically a folder structure containing an executable file):
cd /Applications mkdir -p spyder.app/Contents/MacOS echo -e '#!/bin/bash'"\n /usr/local/bin/spyder3 $@" >> spyder.app/Contents/MacOS/spyder chmod +x spyder.app/Contents/MacOS/spyderCreate a plain text file called Info.plist in the Contents folder (i.e. at spyder.app/Contents/Info.plist) with the following content:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd"> <plist version="0.9"> <dict> <key>CFBundleExecutable</key> <string>spyder</string> <key>CFBundleInfoDictionaryVersion</key> <string>3.1.4</string> <key>CFBundlePackageType</key> <string>APPL</string> <key>CFBundleSignature</key> <string>????</string> <key>CFBundleVersion</key> <string>3.1.4</string> </dict> </plist>(Optional) Create an app icon like in steps 4 and 5 of the accepted answer.
创建一个 Mac 应用程序包(基本上是一个包含可执行文件的文件夹结构):
cd /Applications mkdir -p spyder.app/Contents/MacOS echo -e '#!/bin/bash'"\n /usr/local/bin/spyder3 $@" >> spyder.app/Contents/MacOS/spyder chmod +x spyder.app/Contents/MacOS/spyder在 Contents 文件夹(即在 spyder.app/Contents/Info.plist)中创建一个名为 Info.plist 的纯文本文件,内容如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist SYSTEM "file://localhost/System/Library/DTDs/PropertyList.dtd"> <plist version="0.9"> <dict> <key>CFBundleExecutable</key> <string>spyder</string> <key>CFBundleInfoDictionaryVersion</key> <string>3.1.4</string> <key>CFBundlePackageType</key> <string>APPL</string> <key>CFBundleSignature</key> <string>????</string> <key>CFBundleVersion</key> <string>3.1.4</string> </dict> </plist>(可选)像已接受答案的第 4 步和第 5 步一样创建一个应用程序图标。
Voilà, spyder has become suddenly much more Mac-friendly!
瞧,spyder 突然变得对 Mac 更友好了!
EDIT:
One can further improve the mac-friendliness of spyder by:
编辑:
可以通过以下方式进一步提高 spyder 的 mac 友好性:
Preventing the generic python rocket icon to appear besides the spyder icon
For this, uncheck in spyder the option Tools -> Preferences -> iPython console -> Graphics -> Activate (matplotlib)Making spyder the default editor for .py files.
This one is more tricky. First make sure that the content of spyder.app/Contents/MacOS/spyder reads#!/bin/bash /usr/local/bin/spyder $@
防止通用 python 火箭图标出现在 spyder 图标之外
为此,在 spyder 中取消选中选项 Tools -> Preferences -> iPython console -> Graphics -> Activate (matplotlib)使 spyder 成为 .py 文件的默认编辑器。
这个比较棘手。首先确保spyder.app/Contents/MacOS/spyder的内容为#!/bin/bash /usr/local/bin/spyder $@
Create then an automator script containg a single action "Execute a shell script". Paste the following (bash) script in it:
然后创建一个包含单个操作“执行 shell 脚本”的自动程序脚本。将以下 (bash) 脚本粘贴到其中:
for f in "$@"
do
open /Applications/spyder.app --args $f
done
if [ -z "$f" ]; then
open /Applications/spyder.app --args ~
fi
Choose "As argument" for the input data, as shown in the screenshot below (the "--args ~" is missing in the screenshot but it is needed to avoid an error when launching spyder without any file).

Save this script as an application called "SpyderOpener" for instance.
为输入数据选择“作为参数”,如下面的屏幕截图所示(屏幕截图中缺少“--args ~”,但需要避免在没有任何文件的情况下启动 spyder 时出错)。
例如,将此脚本保存为名为“SpyderOpener”的应用程序。
Make SpyderOpener the default application for opening .py files (by using the Finder "Get Info" dialog on a .py file)
使 SpyderOpener 成为打开 .py 文件的默认应用程序(通过在 .py 文件上使用 Finder“获取信息”对话框)
回答by cookiecook
A very simple method not requiring use of Terminal is essentially to create a spyder.app package in Applications and copy the spyder exec file from Anaconda into this folder. Clicking on the package will run spyder as an app, and you can right-click the dock icon to keep it in the dock.
一个不需要使用终端的非常简单的方法本质上是在 Applications 中创建一个 spyder.app 包,并将 spyder exec 文件从 Anaconda 复制到这个文件夹中。单击包将作为应用程序运行 spyder,您可以右键单击停靠栏图标将其保留在停靠栏中。
Below are the step-by-step instructions.
以下是分步说明。
We first create the spyder.app folder in Applications:
我们首先在 Applications 中创建 spyder.app 文件夹:
Open the Applications folder in Finder.
Right-click and create "New Folder".
- Name the folder "spyder.app" and allow the .app extension.
在 Finder 中打开应用程序文件夹。
右键单击并创建“新文件夹”。
- 将文件夹命名为“spyder.app”并允许使用 .app 扩展名。
We then copy the exec spyder file:
然后我们复制 exec spyder 文件:
In a new Finder window, open your home folder (it should have a simple picture of a house as the icon). A shortcut is to press shift+command+H while Finder is open.
Open the anaconda3 folder, and then open the folder named 'bin'.
- Locate the exec file named 'spyder', and right-click to copy the file.
在新的 Finder 窗口中,打开您的主文件夹(它应该有一个简单的房子图片作为图标)。一个快捷方式是在 Finder 打开时按 shift+command+H。
打开anaconda3文件夹,然后打开名为'bin'的文件夹。
- 找到名为“spyder”的 exec 文件,然后右键单击以复制该文件。
Note: if anaconda3 isn't installed in your home directory, just search for 'anaconda3' with exact filename matches in Finder and open the file.
注意:如果 anaconda3 未安装在您的主目录中,只需在 Finder 中搜索具有精确文件名匹配的“anaconda3”并打开该文件。
Paste the copied exec file into the spyder.app package:
将复制的 exec 文件粘贴到 spyder.app 包中:
In the Finder window open to Applications, right-click the spyder.app created above and select "Show Package Contents".
Right-click in the window and paste the exec file.
在打开应用程序的 Finder 窗口中,右键单击上面创建的 spyder.app 并选择“显示包内容”。
在窗口中右键单击并粘贴 exec 文件。
Now, when you return to the Applications folder, double-click on the spyder.app package and spyder should run. Right-click the icon in the dock and select "Keep in Dock" to keep the app in the dock when not running. You can also create the icon for the app as indicated in 4 and 5 of the original answer.
现在,当您返回到 Applications 文件夹时,双击 spyder.app 包,spyder 应该会运行。右键单击 Dock 中的图标并选择“Keep in Dock”以在不运行时将应用程序保留在 Dock 中。您还可以按照原始答案的 4 和 5 中的指示为应用程序创建图标。
回答by ali HASHEMI
I believe that the simplest way for access to spyder in macOS is via terminal! you keep in the dock the terminal and when you want to open spyder, you open terminal and type spyder in a terminal and then return key! I like this method because when I open a terminal and I press up arrow once or few times I will access to spyder and run it with press return! I know that it is a very obvious and easy method but I wanted to remind it!
我相信在 macOS 中访问 spyder 的最简单方法是通过终端!你把终端放在码头上,当你想打开 spyder 时,你打开终端并在终端中输入 spyder 然后返回键!我喜欢这种方法,因为当我打开终端并按向上箭头一次或几次时,我将访问 spyder 并按回车键运行它!我知道这是一个非常明显和简单的方法,但我想提醒一下!
回答by ryan123
A super quick way with no skill at all is to find the exe (which sypderwill achieve this) in something like //anaconda3/bin/spyder. Duplicate this file and put it on your desktop. Whenever you need it just double click.
一个完全没有技巧的超级快速方法是which sypder在类似//anaconda3/bin/spyder. 复制此文件并将其放在桌面上。需要时只需双击即可。
Note the reason I suggest this as an alternative is because on my work machine, I do not have access to change anything the Applications directory.
请注意,我建议将此作为替代方案的原因是因为在我的工作机器上,我无权更改 Applications 目录的任何内容。
Edit:An even faster way would just be to do:
command+ spacebarthen type Spyder.
编辑:一种更快的方法就是:
command+spacebar然后输入 Spyder。

