Home > Backend Development > Python Tutorial > Set up the Python development environment for the Sublime Text editor

Set up the Python development environment for the Sublime Text editor

巴扎黑
Release: 2017-04-05 15:07:35
Original
1397 people have browsed it

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

My Sublime Text setup

Why choose Sublime Text?

I have always been a loyal user of TextMate. This is a lightweight, open source software that works 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 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 the other day and there is absolutely no regret.

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 are serious about choosing a font, this article from slant.co is a good one. It contains screenshots and download links for most popular programming fonts.

Install plug-in

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

  • Package Control is a package manager for installing add-on plugins 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 affect 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 The default autocomplete only focuses on words in the current file. This plugin extends its autocomplete word list to all open files.


  • SublimeCodeIntel enhances auto-completion functionality 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 from the editing interface. I tend to run bpython in a separate terminal window, but sometimes SublimeREPL can be helpful.


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


  • The Pylinter 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 JSON-based configuration files. 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 use 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"
    ]
}
Copy after login

Pylinter.sublime-settings Configures the pylinter plug-in. 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
}
Copy after login

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"}
]
Copy after login

Command line tools

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 make it take effect, run the following in any shell:

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

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"
Copy after login

More inspiration

I hope this installation guide can help you. If you have any suggestions or comments, please tweet me or send me an email. Also, thanks to the author below and his work on configuring sublime. They inspire me a lot:

  • Kenneth Reitz: Sublime Text 2 Love


  • Drew Barontini: Sublime (2)


  • Filippo Pacifici: Python development with Sublime Text 2 tips and tricks


  • opensourcehacker.org: Sublime Text 2 tips for Python and web developers

The above is the detailed content of Set up the Python development environment for the Sublime Text editor. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template