Ways to invoke python and Spyder on OSX

63,435

Solution 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
    

    (if you use a different shell (e.g. tcsh), replace bash by that)

  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.

Solution 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). automator script
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)

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

Solution 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)

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

Share:
63,435

Related videos on Youtube

kuzzooroo
Author by

kuzzooroo

Updated on March 14, 2022

Comments

  • kuzzooroo
    kuzzooroo about 2 years

    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.

    • asmeurer
      asmeurer over 10 years
      What if you associate .py files with /Users/kuzzooroo/anaconda/python.app (without digging into Contents)?
    • kuzzooroo
      kuzzooroo over 10 years
      @asmeurer, when I associate .py files with /Users/kuzzooroo/anaconda/python.app I can see some indications that the script is trying to run but it doesn't get off the ground. The icons in the dock start moving apart to make room. But before they get all the way apart the process seems to fail and the icons in the dock squish back into place. Unfortunately I'm not sure where to look for an error message so I don't know how to debug it.
    • asmeurer
      asmeurer over 10 years
      You might also find an answer to this question on the Mac stack exchange.
  • kuzzooroo
    kuzzooroo over 10 years
    Thanks. How did you find this? Is there some way I can configure Spotlight so that this would have shown up for me when I searched for "spyder"?
  • Peter Wang
    Peter Wang over 10 years
    Well, Python packages with executable scripts will always get their scripts installed into ~/anaconda/bin. According to apple.stackexchange.com/questions/55116/…, you cannot explicitly add folders to Spotlight. As the commenters on that thread indicate, Spotlight is pretty restrictive, and I certainly would recommend using Quicksilver or Alfred.
  • kuzzooroo
    kuzzooroo almost 10 years
    Quicksilver has been seemingly freezing on me and I finally realized (after an embarassingly long time) that running Spyder was the cause. Quicksilver was starting Spyder as a shell command, and so waiting for it to finish before it (Quicksilver) wouldn't respond to any more input. I wrote a bash script that runs ~/anaconda/bin/spyder > /dev/null 2>&1 & and I invoke that script from Quicksilver instead. That solved the problem.
  • kuzzooroo
    kuzzooroo almost 9 years
    Very nice! Note for other bash users: I had to put #!/bin/tcsh at the top of my script to get it to run correctly.
  • asmeurer
    asmeurer almost 9 years
    Wow, I would have expected Quicksilver to do that automatically. Maybe raise an issue with the Quicksilver developers.
  • Thomas
    Thomas almost 9 years
    @kuzzooroo: Thank you for your remark! I edited that line such that it should work as a regular bash script now.
  • kuzzooroo
    kuzzooroo over 8 years
    Thanks. Unfortunately this has the same drawbacks for me as Thomas's solution: when the program is running it's icon is a generic Python icon, not the Spyder icon, and I still get a second, shell-script-ish icon in the doc as well. Still I think some people may prefer it as a more Apple-ish (and less Unix-ish) solution.
  • Joseph Wood
    Joseph Wood almost 8 years
    @Thomas, excellent answer. Thanks for including such a thorough solution!
  • kuzzooroo
    kuzzooroo almost 7 years
    This is great except it leaves a Python process (with a generic icon) running on the dock in addition to Spyder. Any idea how to stop that from happening?
  • topoman
    topoman over 6 years
    @kuzzooroo: This additional icon is due to matplotlib being loaded "interactively" by spyder upon startup. In Spyder, if you uncheck the option Tools -> Preferences -> iPython console -> Graphics -> Activate (matplotlib), this annoying icon won't appear anymore.
  • Louis Yang
    Louis Yang almost 6 years
    The $@ in echo -e '#!/bin/bash'"\n /usr/local/bin/spyder3 $@" will generate an error when opening Spyder from the Launchpad. However, if I use & then there is no issue.
  • cookiecook
    cookiecook over 5 years
    While you can open the app with no problem, one drawback I notice is that empty folders keep being created in the home directory each time the app is opened - not sure how to fix this. Does anyone happen to know?
  • Javier Lopez Tomas
    Javier Lopez Tomas about 5 years
    Is this still necessary nowadays? Thanks
  • abegehr
    abegehr about 4 years
    link to icon is broken. svg (needs to be exported to png first) can be found here: spyder-ide.org/static/images/spyder-logo.svg
  • Thomas
    Thomas about 4 years
    Thanks, I updated the link to a directory that also includes the .png file.
  • Vik
    Vik almost 4 years
    I was able to do this successfully with Spyder 3, however the latest version 4.1.3 with Anaconda 2020.02 doesn't work this way. Has anyone else been able to get that to work?
  • Mathieu
    Mathieu over 3 years
    Works very well except the last part where the file pyder.app/Contents/MacOS/spyder reads /usr/local/bin/spyder3 on the second line.
  • Will Townes
    Will Townes over 3 years
    I tried this on Catalina and the app opens, but as soon as the ipython kernel finishes loading the app just crashes and disappears. Any suggestions?
  • Mathieu
    Mathieu over 3 years
    @WillTownes I think I have the same now, despite it working as intended at first. I am now starting spyder through terminal with the command spyder3. I made an alias to remove the 3 in .zshrc.
  • Noam Suissa
    Noam Suissa over 3 years
    Great solution. I am unable to put in the dock. Is there a solution for this?
  • Brian Wiley
    Brian Wiley about 3 years
    With this answer I get error IPython 7.19.0 -- An enhanced Interactive Python. Error in callback <function sys_pipes_forever at 0x7fbdc1059700> (for pre_execute): error but not with answer below using the bundle
  • jaweej
    jaweej almost 3 years
    I attempted steps 1-3 above, but it only worked after I'd run conda install -f python.app
  • Niuya
    Niuya almost 2 years
    Able to launch the spyder 5.1.5, but there is an error on IPython console and block its usability.