Ways to invoke python and Spyder on OSX

狂风中的少年 提交于 2019-12-03 00:29:54

问题


I recently bought a MacBook and install Python on it via Anaconda. Here's the version information:

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:

  1. Launch Spyder without having to go through the Launcher application (say, by typing things into Spotlight and Quicksilver)
  2. Run Python code from the Finder, presumably by double-clicking on my .py file or by selecting and and pressing Cmd-O
  3. Run Python code by typing a Python script's name into Quicksilver (as if it was a regular program)

Here's what I've tried:

  1. (nothing; no ideas here)
  2. 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)
  3. Made the files executable and tried to run them from Spotlight. Same result as #2.

Here's what I can do:

  • 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

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.


回答1:


To make spyder callable from Spotlight or Finder:

  1. Locate where your spyder executable is by running in Terminal:

    which spyder
    

    This should yield ~/anaconda/bin/spyder if you installed spyder via Anaconda, /opt/local/bin/spyder if you used MacPorts or something similar.

  2. Create a file called spyder in your Applications directory 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
    
  3. Under Terminal -> Preferences -> Profiles -> "default profile" -> Shell -> When the shell exits: Select "Close if the shell exited cleanly"

Optional:

  1. Download the spyder Icon from here and open it in Preview. Copy its contents by hitting cmd+C.

  2. In Finder, locate /Applications/spyder and open its "Get Info" pane by hitting cmd+I. Select the icon in the top left corner with your mouse and hit cmd+V.




回答2:


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.

To convert /usr/local/bin/spyder3 (the result of which spyder3 on my machine) into a traditional Mac application:

  1. 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/spyder
    
  2. Create 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>
    
  3. (Optional) Create an app icon like in steps 4 and 5 of the accepted answer.

Voilà, spyder has become suddenly much more Mac-friendly!

EDIT:
One can further improve the mac-friendliness of spyder by:

  1. 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)

  2. 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 $@
    

Create then an automator script containg a single action "Execute a shell script". Paste the following (bash) script in it:

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.

Make SpyderOpener the default application for opening .py files (by using the Finder "Get Info" dialog on a .py file)




回答3:


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.




回答4:


You can use Automator to create an Application that will run Unix Script. Open Automator when it asks for document type put "Application"

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.

(I did this with Mac OS 10.10 and a Anaconda 2.3.0)




回答5:


You can open spyder by pointing Quicksilver to ~/anaconda/bin/spyder (you can add it to your catalog so that it always finds it).




回答6:


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.

Below are the step-by-step instructions.

We first create the spyder.app folder in Applications:

  1. Open the Applications folder in Finder.

  2. Right-click and create "New Folder".

  3. Name the folder "spyder.app" and allow the .app extension.

We then copy the exec spyder file:

  1. 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.

  2. Open the anaconda3 folder, and then open the folder named 'bin'.

  3. Locate the exec file named 'spyder', and right-click to copy the file.

Note: if anaconda3 isn't installed in your home directory, just search for 'anaconda3' with exact filename matches in Finder and open the file.

Paste the copied exec file into the spyder.app package:

  1. In the Finder window open to Applications, right-click the spyder.app created above and select "Show Package Contents".

  2. Right-click in the window and paste the exec file.

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.




回答7:


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!




回答8:


A super quick way with no skill at all is to find the exe (which sypder will achieve this) in something like //anaconda3/bin/spyder. Duplicate this file and put it on your desktop. Whenever you need it just double click.

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.

Edit: An even faster way would just be to do: command + spacebar then type Spyder.



来源:https://stackoverflow.com/questions/20723977/ways-to-invoke-python-and-spyder-on-osx

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