杰瑞科技汇

Sublime Text下Python环境如何配置?

Of course! Using Python with Sublime Text is a very popular and powerful combination. It's a lightweight, fast, and highly customizable code editor that's excellent for Python development.

Sublime Text下Python环境如何配置?-图1
(图片来源网络,侵删)

Here’s a comprehensive guide covering everything from setup to advanced tips.


Why Use Sublime Text for Python?

  • Speed: Sublime Text is incredibly fast, even with large files and projects.
  • Lightweight: It's less resource-intensive than full IDEs like PyCharm or VS Code, making it great for older machines or quick edits.
  • Customization: Nearly everything can be customized—from keybindings and menus to syntax highlighting and behavior—using simple JSON and Python scripts.
  • Goto Anything: The Ctrl+P (or Cmd+P on Mac) command is legendary. It lets you instantly open any file in your project by typing its name.
  • Multiple Cursors & Selections: Easily edit multiple lines of code at once, a huge time-saver.
  • Powerful Package Ecosystem: A rich set of community-built packages (plugins) extends its functionality to rival full IDEs.

Installation and Setup

Step 1: Install Sublime Text

Step 2: Install the Python Package

The "Python Package" is a plugin that provides language features like linting, code formatting, and auto-completion.

  1. Open the Command Palette: Press Ctrl+Shift+P (or Cmd+Shift+P on Mac).
  2. Type Install Package and select it.
  3. Wait for the package list to load (it might take a moment the first time).
  4. Type Python and select the one by Sublime HQ Pty Ltd. This is the official and most recommended package.

Step 3: Install a Linter (Highly Recommended)

A linter analyzes your code for potential errors, style issues, and bugs. The most popular linter for Python is Flake8.

  1. Install the Linter package via the Command Palette (Ctrl+Shift+P -> Install Package -> Linter).
  2. Install Flake8 using pip (Python's package installer). Open your system's terminal or command prompt and run:
    pip install flake8
  3. Configure Sublime Text to use Flake8:
    • Go to Preferences > Settings (or use the shortcut Ctrl+,).
    • In the user settings file (the one on the right), add or modify the following line:
      {
      "linters": {
          "flake8": {
              "executable": "flake8",
              "args": ["--max-line-length=88"]
          }
      }
      }
    • Now, when you save a Python file (Ctrl+S), Sublime Text will automatically run Flake8 and show you any issues in the status bar and with red squiggly lines in the code.

Essential Packages for Python Development

The real power of Sublime Text comes from its packages. Here are the must-haves for a great Python experience.

Sublime Text下Python环境如何配置?-图2
(图片来源网络,侵删)
Package Name What it Does How to Install
Anaconda The ultimate Python package. It combines linters, auto-completion, code formatting, and a powerful Python console. It can replace the Python and Linter packages. Ctrl+Shift+P -> Install Package -> Anaconda
SublimeLinter The core plugin that runs linters (like Flake8) and displays errors. Often installed as a dependency for other linters. Ctrl+Shift+P -> Install Package -> SublimeLinter
Linter-flake8 The Flake8 linter plugin for SublimeLinter. Ctrl+Shift+P -> Install Package -> Linter-flake8
GitGutter Shows git diff markers in the gutter (the bar on the left) of your editor. You can see added, modified, and deleted lines at a glance. Ctrl+Shift+P -> Install Package -> GitGutter
Package Control You already have this! This is the most important package. It's the package manager for Sublime Text. It's installed by default in recent versions. -
DocBlockr Helps you write better docstrings. It auto-generates them based on your function's arguments and lets you navigate between them easily. Ctrl+Shift+P -> Install Package -> DocBlockr
SublimeREPL Run Python code or an interactive shell inside Sublime Text. Great for quick tests without leaving the editor. Ctrl+Shift+P -> Install Package -> SublimeREPL

Running Python Code

There are two main ways to run your Python scripts.

Method 1: Using the Build System (Recommended)

Sublime Text has a built-in build system for Python.

  1. Save your file with a .py extension (e.g., hello.py).
  2. Write your code:
    print("Hello from Sublime Text!")
    name = "Developer"
    print(f"Hello, {name}!")
  3. Run it by pressing Ctrl+B (or Cmd+B on Mac).

The output will appear in a new "output" panel at the bottom of the screen.

Troubleshooting: If you get an error like python: command not found, Sublime Text can't find your Python interpreter. You can fix this by telling it the path.

Sublime Text下Python环境如何配置?-图3
(图片来源网络,侵删)
  • Go to Tools > Build System > New Build System...
  • A new file will open. Replace the contents with this:
    {
        "cmd": ["C:/Python39/python.exe", "-u", "$file"],
        "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
        "selector": "source.python"
    }
    • Important: Change "C:/Python39/python.exe" to the actual path of your Python executable. On Windows, it might be C:\Users\YourUser\AppData\Local\Programs\Python\Python39\python.exe. On Mac/Linux, it's often /usr/bin/python3 or /usr/bin/python.
  • Save the file as Python.sublime-build in the default location.
  • Now, go to Tools > Build System and select your new Python build system.

Method 2: Using SublimeREPL

  1. Install the SublimeREPL package.
  2. Open your Python file.
  3. Open the Command Palette (Ctrl+Shift+P).
  4. Type SublimeREPL: Python - RUN current file and press Enter.
  5. A new interactive terminal will open at the bottom of the screen, and your script will run in it.

Useful Tips and Tricks

  • Go to Definition: With the Anaconda package installed, Ctrl+Click (or Cmd+Click) on a function or variable to jump to its definition.
  • Multiple Cursors: Hold Ctrl and click to place multiple cursors. Or, select a word with Ctrl+D, then continue pressing Ctrl+D to select the next occurrence.
  • Column Selection: Hold Shift+Alt and drag your mouse to select a vertical block of text.
  • Command Palette is Your Best Friend: Almost every action can be accessed via Ctrl+Shift+P. Don't try to memorize all the menus.
  • Snippets: Create your own code snippets for things you type often (like for loops, if __name__ == "__main__":, etc.). Go to Tools > Developer > New Snippet... to get started.
  • Customize Theme and Color Scheme: Go to Preferences > Color Scheme or Preferences > Theme to change the look and feel. There are thousands available via Package Control.

Sublime Text vs. VS Code

This is a common comparison. Here's a quick breakdown to help you choose.

Feature Sublime Text VS Code
Performance Blazingly fast. Almost instant startup and editing. Very fast, but can be slower with very large workspaces due to its heavier feature set.
Customization Extensive, but requires editing JSON files and writing Python plugins. Extensive, but often done through a graphical UI and a rich marketplace of extensions.
Integrated Terminal Good, but not as deeply integrated as VS Code. Excellent. The terminal is a first-class citizen, easily accessible and manageable.
Debugging Possible via plugins, but can be complex to set up. Excellent. A fully-featured, integrated debugger is out of the box.
Learning Curve Steeper. You need to learn the keybindings and configuration system. Gentler. The UI is familiar to many, and the extension system is easy to use.
Price Free to evaluate, but requires a license for continued use (you can still use it without a license, but a pop-up appears occasionally). Free and open-source.

Conclusion:

  • Choose Sublime Text if: You prioritize raw speed, a minimal interface, and love to tinker and customize your tools from the ground up. It's a "programmer's editor" in the purest sense.
  • Choose VS Code if: You want a more "out-of-the-box" experience with excellent built-in features like a debugger, integrated terminal, and a massive extension library with easy-to-use graphical configuration.
分享:
扫描分享到社交APP
上一篇
下一篇