Ways to invoke python and Spyder on OSX

前端 未结 8 1909
深忆病人
深忆病人 2020-12-07 10:00

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 2         


        
8条回答
  •  情书的邮戳
    2020-12-07 10:43

    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:

      
      
      
      
      CFBundleExecutable
      spyder
      CFBundleInfoDictionaryVersion
      3.1.4
      CFBundlePackageType
      APPL
      CFBundleSignature
      ????
      CFBundleVersion
      3.1.4
      
      
      
    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)

提交回复
热议问题