翻譯:打造基於Sublime Text 3的全能python開發環境


原文地址:https://realpython.com/blog/python/setting-up-sublime-text-3-for-full-stack-python-development/

原文標題:Setting Up Sublime Text 3 for Full Stack Python Development

翻譯:打造基於sublime text 3的全能Python開發環境

Sublime Text 3 (ST3) is lightweight, cross-platform code editor known for its speed, ease of use, and strong community support. It’s an incredible editor right out of the box, but the real power comes from the ability to enhance its functionality using Package Control and creating custom settings.

In this article, we’ll look at how to setup Sublime Text for full stack Python development (from the front to back), enhance the basic functionality with custom themes and packages, and use many of the commands, features, and keyword shortcuts that make ST3 so powerful.

ST3是一個以運行速度,易用性和強大社區支持而著稱的輕量級跨平台代碼編輯器。它是一個難以置信的開箱即用的編輯器,但是通過包管理器和自定義設置來增強本身的功能其實st3強大力量的來源。

本文將介紹如何為全方位的Python開發(從前段到后端)配置ST3,通過自定義主題和擴展報來增強基本功能,大量命令,特性和基於關鍵詞快捷鍵的使用讓st3變得十分強大.

This tutorial assumes you’re using a Mac and are comfortable with the terminal. If you’re using Windows or Linux, many of the commands will vary, but you should be able to use Google to find the answers quickly given the info in this tutorial.

本教程假設你用的是Mac操作系統並且能夠熟練的使用命令行終端.如果你用的是windows或者Linux操作系統,有些命令可能會不一樣,不過你應該可以通過google來找到如何在你自己系統中使用本教程提到的命令.

Before we start, let’s address what I mean exactly by “full stack”.

In today’s world of HTML5 and mobile development, Javascript is literally everywhere. EVERYWHERE. Python coupled with a framework such as Django or Flask is not enough. To really develop a website from end-to-end, you must be familiar with Javascript (and the various Javascript frameworks), REST APIs, responsive design, and of course HTML and CSS, among others. And let’s face it: Like any craftsman, in order to be the best programmer you can be, your tools must be sharp. Your development environment must be setup for full stack development – which is exactly what we are going to do right now…

在開始本教程之前,讓我們來明確一下我所指的全方位(full stack)是什么意思.在當今的HTML5和移動設備開發中,javascript被廣泛使用.everywhere!python結合諸如Django和Flask之類的框架是不能滿足需求的.為了開發一個端到端的網站,你必須熟悉javascript及其大量的js框架,rest apis,響應式設計,html&css和其他.為了成為最好的程序員,工欲善其事必先利其器!你必須把你的開發環境配置成適應全方位開發,這就是我們接下來將要做的

Features

Let’s start by looking at a few of the default features of Sublime Text 3…

Split Layouts allow you to arrange your files in various split-screens. This is useful for test driven development (Python code on one screen, test scripts on another) or when working on the front end (HTML on one screen, CSS and/or Javascript on another).

特性

首先我們來看sublime text 3提供的一系列默認的特性

分割布局允許你用多種屏幕分割方式來組織代碼.在測試驅動的開發中特別用用(python代碼在一個屏幕,測試腳本在另外一個)或者前段開發(html在一個屏幕,css和js在另一個)特別游泳.

  1. Vintage Mode provides you with vi commands for use within ST3.

  2. Chrome-like Tabs make navigating and editing several files much simpler.
  3. Automatic loading of the last session re-opens all files and folders you had open when you closed the editor the last time. I leave ST3 open all the time, with various projects open – so if I reset the computer, it opens the files and folders right back up.
  4. Code Snippets increase your productivity by giving you the ability to create common pieces of code with a single keyword. There are a number of default snippets. For example, open a new file and type in “lorem” then press tab. You should get a paragraph of lorem ipsum text. Also, if you type “defs” then press tab in a Python file it will setup a generic function.

    You can also create your own snippets: Tools > New Snippet. Refer to the documentation for help, and also check out some of my snippets here.

  1. Vintage模式允許你在st3中使用vi命令
  2. Chrome-like標簽也讓多文件導航和編輯變得更加容易
  3. 自動加載上一次會話,自動打開你上次關閉st3之前打開的文件和文件夾.作者st3常開,並且同時打開多個項目,如果重啟電腦,立刻打開備份的文件和文件夾
  4. 代碼片段讓你能夠通過一個關鍵詞來創建代碼段來提高你的效率.有很多默認的代碼片段.例如,新建一個文件,輸入lorem,按tab鍵,你將會得到一大段文字.如果你在python文件中輸入defs並且敲tab會的到一個通用的函數體.

Customizing Sublime Text 3

After downloading ST3 …

Install the subl command line tool

Like the mate command for TextMate, Sublime Text includes a command line tool called subl that allows you to open one file, or an entire directory of files and folders, from the terminal.

To enable this command, create a symbolic link to the subl binary:

定制st3

下載st3之后

安裝subl命令行工具

就像TextMate的mate命令,sublime text包含了一個叫做讓你能夠從終端打開一個文件或者一系列文件文件夾的命令行工具

為了使用這個命令,建立一個到subl庫的符號鏈接

$ sudo ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/bin/subl

 Ensure that the link works by opening Sublime:

通過打開sublime來確認鏈接可用

$ subl

 If that didn’t work, you probably need to add /bin to your Path:

如果subl不起作用,你可能需要把/bin添加到你的環境變量

$ echo "export PATH=~/bin:$PATH" >> ~/.profile

 現在你可用下面的命令打開文件和目錄:

# open the current directory
$ subl .

# open a directory called tests
$ subl ~/Documents/test

# open a file called text.txt
$ subl test.txt

譯注:我用的ubutnu 15.04安裝st3的deb包,命令行下直接就可用使用subl,所以這一步可用省略

$ subl hello.py  # 直接就可以在sublime里打開,不用設置
$ subl "hello world" # 如果路徑中有空格,路徑必須加引號
$ subl --help # 可用查看所有的命令

Install Package Control

To begin taking advantage of the various packages for extending Sublime’s functionality, you need to install the package manager called Package Control – which you must install manually. Once installed, you can use Package Control to install/remove/upgrade all other ST3 packages.

  1. To install, copy the Python code for Sublime Text 3 found here. Click View > Show Console to open the ST3 console. Paste the code into the console. Press enter. Reboot ST3.

  2. You can now install packages by using the keyboard shortcut cmd+shift+P. Start typing install until Package Control: Install Package appears. Press enter and search for available packages.

  3. Some other relevant commands are:

    • List Packages shows all your installed packages
    • Remove Packages removes a specific package
    • Upgrade Package upgrades a specific package
    • Upgrade/Overwrite All Packages upgrades all your installed packages

     

    Check out the official documentation to view more commands.

 安裝包管理器為了利用各種擴展sublime功能的包,你需要手動安裝一個叫做Package Control的包管理器.安裝之后,你可以通過它來安裝/卸載/升級所有st3包(插件).

  1. 為了安裝插件你必須復制這里為st3設計的python代碼.單擊視圖→顯示控制台來打開st3控制台,把代碼粘貼到控制帶,回車並且重啟st3
  2. 你現在可用通過ctrl+shift+p來安裝插件.按快捷鍵后輸入instal,直到Package Control: Install Package出現.敲回車並且搜索可用的插件.
  3. 一些其他的命令包括
    1. 列出你已經安裝的插件
    2. 刪除一個特定的插件
    3. 升級特定的插件
    4. 升級/覆蓋所有包 升級你所有的插件

       

查看官方文檔documentation獲取更多命令.

Create a Custom Settings File

You can fully configure Sublime Text using JSON-based settings files, making it easy to transfer, or synchronize, your customized settings to another system. First, we need to create our customized settings. It’s best to create a base file for all environments as well as language-specific settings files.

To set up a base file click Sublime Text > Preferences > Settings – User. Add an empty JSON object to the file and add your settings like so:

新建自定義配置文件

你可以通過json格式配置文件自由的配置sublime text,采用json格式使得配置文件易於傳輸,或者同步你的配置到其他系統.首先我們需要創建自己的自定義設置.建議分別設置針對所有的環境的基本配置文件和針對特定語言的配置文件.

通過sublime text→選項→配置-用戶來創建一個基本配置.在文件中添加一個空的json對象,並且添加配置

{
  // base settings
  "auto_complete": false,
  "sublimelinter": false,
  "tab_size": 2,
  "word_wrap": true
}

 

  1. 特定語言的配置,Sublime Text > Preferences > Settings – More > Syntax Specific – Use,用語言名.sulime-settings來保存,比如針對python的配置文件就是Python.sublime-settings
  2. 你可以對st做任意配置,但是我強力推薦你從配置基本配置和針對python的配置開始,接下來你將會看到改變
  3. 可選:你可以用Dropbox來同步你所有的配置,把文件上傳到Dropbox並且在所有的電腦上加載並同步sublime環境.
  4. 一個很好的sublime配置參考Sublime Text Unofficial Documentation.

 

Themes

ST3 also gives you the option to change the overall theme to better suit your personality. Design your own. Or, if you’re not artistically inclined, you can download one of the various custom themes designed by the Sublime community through Package Control. Check out ColorSublime to preview themes before installing them.

The ever popular Soda Dark Theme and the minimal Flatland are two of my personal favorites.

After installing a theme, make sure to update your base settings, Sublime Text > Preferences > Settings – User:

Packages

除了打包的主題,我也用下面這些插件來增加速我的工作流.

SideBarEnhancements側邊欄增強

SideBarEnhancements extends the number of menu options in the sidebar, speeding up your overall workflow. Options such as “New file” and “Duplicate” are essential and should be part of ST3 out of the box. The “Delete” option alone makes it worth downloading. This feature simply sends files to the Trash, which may seem trivial but if you delete a file without it, it’s very difficult to recover unless you’re using a version control system.

這個插件擴展了側邊欄冊單的數量,加速你算不的工作流.比如新建和復制是st3自帶的基本選項,而"刪除"選項讓這個插件值得下載.這個功能把文件放到回收站看起來不是很重要的功能,但是你不用這個選項刪除文件,除非你使用了版本控制系統否則你很難恢復文件.

Anaconda

Anaconda是一個旗艦版的Python插件,給st3添加了一系列類似IDE的功能,如:

  • Autocompletion works by default, but there are a number of configuration options.默認自動完成,並且有一些配置選項
  • Code linting uses either PyLint or PyFlakes with pep8. I personally use a different linting package, as I will explain shortly, so I disable linting altogether within the user-defined Anaconda settings file, Anaconda.sublime-settings, via the file menu: Sublime > Preferences > Package Settings > Anaconda > Settings – User: {"anaconda_linting": false} 代碼檢查,用pylint或者pyflakes及pep8,作者使用不同的代碼檢查插件,所以作者簡單的通過Anaconda用戶配置文件來禁用Anaconda插件自帶的代碼檢查功能
  • McCabe code complexity checker runs the McCabe complexity checker tool within a specific file. If you’re unfamiliar with a complexity checker, be sure to visit the link above.代碼復雜度檢測
  • Goto Definitions finds and displays the definition of any variable, function, or class throughout your entire project.查找並顯示你工程中任何變量,函數和類.
  • Find Usage quickly searches where a variable, function, or class has been used in a specific file.特定文件中已經使用過的變量函數和類.
  • Show Documentation: shows the Docstring for functions or classes (if defined, of course).如果定義了文檔,顯示文檔.

You can view all of the features here, or within the README file in ST3’s Package Settings: Sublime Text > Preferences > Package Settings > Anaconda > README.點擊鏈接查看所有功能,或者查看readme文件.

SublimeCodeIntel is another popular package, which has many of the same features as Anaconda. I suggest testing them both out.

這個插件是另外一個很受歡迎的插件,有很多和Anaconda類似的功能,建議你們都試用一下.

 

Djaneiro

Djaneiro supports Django templating and keyword highlighting and provides useful code snippets (tab completions) for Sublime Text. The snippet system is an incredible timesaver. You can create common Django blocks with only a few keystrokes for templates, models, forms, and views. Check out the official documentation to see a list of snippets.

My personal favorites are for templating: var creates {{ }} and tag creates {% %}

requirementstxt

Requirementstxt provides autocompletion and syntax highlight-lighting as well as a nice version management system for your requirements.txt files.

SublimeLinter

相關閱讀:http://segmentfault.com/a/1190000000389188學習使用sublime插件sublimeLinter

SublimeLinter is a framework for ST3 linters. The package itself does not include any actual linters; those must be installed separately via Package Control using the SublimeLinter-[linter_name] naming syntax. You can view official linters here. There are also a number of third party linters, which can be viewed in Package Control. Check out the installation instructions here.

SublimeLinter是一個ST3代碼檢查框架.插件本身不包含任何檢查器.必須通過插件管理器用SublimeLinter-[linter_name]指定語法.點擊鏈接查看所有官方檢查器.你也可以試用第三方檢查器,在插件管理器力可用查看.點擊鏈接查看安裝指導

For Python linting, I recommend using SublimeLinter-pyflakes and SublimeLinter-pep8.

對於Python代碼檢查,推薦SublimeLinter-pyflakes和SublimeLinter-pep8

I also use SublimeLinter-jshint, SublimeLinter-pyyaml, SublimeLinter-csslint, SublimeLinter-html-tidy, and SublimeLinter-json.

Most of these linters have dependencies associated with them, so please read the installation instructions before installing.

You can customize each linter in the user-defined SublimeLinter.sublime-settings file: Sublime Text > Preferences > Package Settings > SublimeLinter > Settings – User. For example, I ignore the following pep8 errors and warnings:自定義錯誤的方法

"pep8": {
    "@disable": false,
    "args": [],
    "excludes": [],
    "ignore": "E501,C0301,W0142,W0402,R0201,E1101,E1102,C0103,R0901,R0903,R0904,C1001,W0223,W0232,W0201,E1103,R0801,C0111",
    "max-line-length": 100,
    "select": ""
},

 

GitGutter

GitGutter shows little icons in ST3’s gutter area that indicate whether a line has been inserted, modified, or deleted since the last commit.GitGutter 是一個兼容Sublime Text 2 或者Sublime Text 3 的插件,它可以在行數的前面顯示那一行是被增加,修改還是刪除。顯示每行變化的插件.

Emmet

Emmet, previously known as Zen Coding, uses simple abbreviations to generate HTML or CSS code snippets.

For example, if you type a bang, !, then press tab in an HTML file ,the HTML5 doctype and a few basic tags are generated:

<!doctype html> <html lang="en"> <head>  <meta charset="UTF-8">  <title>Document</title> </head> <body>  </body> </html> 

Check out the official documentation as well as this handy cheat sheet for more info.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM