Home >Backend Development >Python Tutorial >Detailed graphic and text explanation of the steps to set up Sublime Text's Python development environment

Detailed graphic and text explanation of the steps to set up Sublime Text's Python development environment

高洛峰
高洛峰Original
2017-03-17 17:16:441649browse

Recently, when I mainly use the Python development environment to edit, I have begun to use Sublinme Text 2 more and more. This article mainly explains how to make Python programmers more convenient to use some settings and adjustments.

图文详解设置Sublime Text的Python开发环境步骤

Why choose Sublime Text?

I have always been a loyal user of TextMate. This is a lightweight, open source software that comes as a native OS X application with a nice Mac feel. However, although TextMate is a great editor, it sometimes lacks functionality.

I have used some more powerful software, such as IntelliJ IDEA with Python plug-in. I particularly like its debugger and test runner. However, a full-featured IDE like IntelliJ is still too large for small and medium-sized projects.

In recent weeks I have started using Sublime Text more and more. Once I installed it, it felt really good. It's really fast, updates automatically and regularly, and best of all, it's fully cross-platform. For me, where it ultimately beats TextMate is Sublime's powerful plugin subsystem. For Python development, there are many plug-ins that can make your development smoother and more fun.

I still switch editors between different projects. However, I found that for Python development, Sublime has a good balance between a lightweight editor and a full-featured IDE.

Font selection

Ubuntu Mono is a very, very good font. I just switched from Menlo a few days ago and I definitely don’t regret it.

On my 15-inch MacBook, Ubuntu Mono’s 16-point font fits perfectly. The resolution of 1680 × 1050 is just right for a sidebar plus two editor windows (which automatically adjust to 80 characters wide).

If you plan to carefully choose a font, this article from slant.co is well written. It contains screenshots and download links for most popular programming fonts.

Install plug-ins

As mentioned before, Sublime has a very rich plug-in system. The plug-ins I currently use are as follows:

  • Package Control The package manager for installing additional plug-ins directly in Sublime. This is the only plugin you have to install manually. All other plugins listed here can be installed via Package Control. You can also use it to update installed plug-ins. It's as simple as apt-get for Sublime packages.

  • Color Scheme - Tomorrow Night Color schemes determine the font color for syntax highlighting in the editor interface. This is a very cool dark style.

  • Theme - Soda Dark Themes affects the color and style of Sublime interface elements. This is a perfect color scheme for Tomorrow Night.

  • SideBarEnhancements This plug-in provides additional context menu options for the sidebar, such as "New file", "New Floder", etc. These should be there by default, but they are not.

  • All Autocomplete Sublime's default autocomplete only focuses on words in the current file. This plugin extends its autocomplete word list to all open files.

  • SublimeCodeIntel enhances the auto-complete function for some languages, including Python. This plugin also allows you to jump to where a symbol is defined by holding down alt and clicking on the symbol. Very convenient.

  • SublimeREPL allows you to run the Python interpreter directly in the editing interface. I prefer running bpython in a separate terminal window, but sometimes SublimeREPL can be helpful.

  • GitGutter In the groove area of ​​the editor, according to Git, add a small icon to identify whether a line is inserted, modified or deleted. The GitGutter readme explains how to change the color icons to update your color scheme file.

  • Pylinter This plug-in provides the best pylint editor integration I have seen so far. It automatically checks .py files whenever they are saved and displays pylint violations directly in the editing interface. It also has a shortcut to disable local pylint checking, by inserting a #pylint: disable comment . This plugin is really useful for me.

Configuration file

One of the advantages of Sublime Text is that all its configurations are simple configurations based on JSON document. This allows you to easily transfer the configuration to another system. I've also seen some people use Dropbox to automatically sync their configuration across all their computers.

 Preferences.sublime-settings configures the display and behavior of Sublimede. You can open and edit this file in sublime through Preferences > Settings — User. I configured the pylinter plug-in using the following configuration:

{
    // Colors
    "color_scheme": "Packages/Tomorrow Color Schemes/Tomorrow-Night.tmTheme",
    "theme": "Soda Dark.sublime-theme",
 
    // Font
    "font_face": "Ubuntu Mono",
    "font_size": 16.0,
    "font_options": ["subpixel_antialias", "no_bold"],
    "line_padding_bottom": 0,
    "line_padding_top": 0,
 
    // Cursor style - no blinking and slightly wider than default
    "caret_style": "solid",
    "wide_caret": true,
 
    // Editor view look-and-feel
    "draw_white_space": "all",
    "fold_buttons": false,
    "highlight_line": true,
    "auto_complete": false,
    "show_minimap": false,
 
    // Editor behavior
    "scroll_past_end": false,
    "highlight_modified_tabs": true,
    "find_selected_text": true,
 
    // Word wrapping - follow PEP 8 recommendations
    "rulers": [ 72, 79 ],
    "word_wrap": true,
    "wrap_width": 80,
 
    // Whitespace - no tabs, trimming, end files with \n
    "tab_size": 4,
    "translate_tabs_to_spaces": true,
    "trim_trailing_white_space_on_save": true,
    "ensure_newline_at_eof_on_save": true,
 
    // Sidebar - exclude distracting files and folders
    "file_exclude_patterns":
    [
        ".DS_Store",
        "*.pid",
        "*.pyc"
    ],
    "folder_exclude_patterns":
    [
        ".git",
        "pycache",
        "env",
        "env3"
    ]
}

Pylinter.sublime-settings. I use the following configuration to have Pyhton automatically normalize on save and display icons for violations.

{
    // Configure pylint's behavior
    "pylint_rc": "/Users/daniel/dev/pylintrc",
 
    // Show different icons for errors, warnings, etc.
    "use_icons": true,
 
    // Automatically run Pylinter when saving a Python document
    "run_on_save": true,
 
    // Don't hide pylint messages when moving the cursor
    "message_stay": true
}

Key binding

Sublime’s key bindings are also fully configurable based on the JSON-based sublime-keymap configuration file. I modified some of the default configuration to better suit my TextMate / IntelliJ muscle memory. You don't have to modify it at all. It's easy to modify and can be used cross-platform if you want. I use the following binding:

[
    // Rebind "go to file" to cmd+shift+O
    { "keys": ["super+shift+o"], "command": "show_overlay", "args": {
        "overlay": "goto",
        "show_files": true
    }},
 
    // Rebind swap line up/down to cmd+shift+up/down
    { "keys": ["super+shift+up"], "command": "swap_line_up" },
    { "keys": ["super+shift+down"], "command": "swap_line_down" },
 
    // Delete a line with cmd+delete
    { "keys": ["super+backspace"], "command": "run_macro_file", "args": {
        "file": "Packages/Default/Delete Line.sublime-macro"
    }},
 
    // Reindent selection with cmd+alt+L
    { "keys": ["super+alt+l"], "command": "reindent"}
]

Command Line Tool

Similar to TextMate's mate, Sublime Text includes a command line tool that allows you to open the editor through a shell. The tool is called sublis and is not available by default. To take effect, run the following in any shell:

ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/local/bin/subl

To use Sublime as the default editor for interactive git commands - for example, writing commit messages - just add the following line to your ~/.profile file:

export GIT_EDITOR="subl --wait --new-window"

The above is the detailed content of Detailed graphic and text explanation of the steps to set up Sublime Text's Python development environment. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn