杰瑞科技汇

Sublime Python Jedi如何自动补全代码?

Of course! This is a classic and powerful combination for Python development in Sublime Text. Here’s a comprehensive guide covering everything from the basics to advanced configuration.

Sublime Python Jedi如何自动补全代码?-图1
(图片来源网络,侵删)

What is Jedi?

First, it's important to understand what Jedi is.

  • Jedi is a static analysis tool for Python. It's not just an auto-completer; it's a full-fledged code intelligence engine.
  • It powers features like autocompletion, go-to-definition, find all references, and symbol lookup (e.g., viewing function signatures).
  • It's designed to be fast and accurate, making it the preferred backend for many popular Python IDEs and editors, including Sublime Text (via a plugin).

The Core Workflow: Sublime Text + Jedi

The standard way to get Jedi working in Sublime Text is through the LSP (Language Server Protocol) ecosystem. This is a modern, robust approach that centralizes language intelligence for many different languages.

Here’s the step-by-step process:

Step 1: Install the Prerequisites

  1. Install Sublime Text: Make sure you have a recent version of Sublime Text (ideally 4 or newer).
  2. Install Package Control: This is the essential package manager for Sublime Text. If you don't have it, follow the official instructions: https://packagecontrol.io/installation

Step 2: Install the LSP Package

The LSP package is the bridge between Sublime Text and the language servers (like Jedi).

Sublime Python Jedi如何自动补全代码?-图2
(图片来源网络,侵删)
  1. Open Sublime Text.
  2. Press Cmd+Shift+P (macOS) or Ctrl+Shift+P (Windows/Linux) to open the Command Palette.
  3. Type Install Package and select it.
  4. Type LSP and install the package by sublimelsp.

Step 3: Install the Jedi Language Server

Now, we'll tell the LSP package to use Jedi for Python files.

  1. Open the Command Palette (Cmd+Shift+P / Ctrl+Shift+P).
  2. Type LSP: Install Server and select it.
  3. A list of available servers will appear. Type jedi and select Jedi Language Server.
  4. Sublime Text will automatically download and configure the server for you.

Step 4: Configure Python to Use Jedi

Sublime Text needs to know which Python interpreter to use for the Jedi server to analyze your code.

  1. Open the Command Palette.

  2. Type LSP: Select LSP Server and select it.

    Sublime Python Jedi如何自动补全代码?-图3
    (图片来源网络,侵删)
  3. You will see a list of installed servers. Find Jedi and select it.

  4. A new menu will appear. Select python.executable.

  5. Sublime Text will now try to find your Python executable. It will likely find your system Python. If you are using a virtual environment, you need to manually point it to the python executable inside that environment's bin/ (macOS/Linux) or Scripts/ (Windows) folder.

    • How to set it for a specific project (Recommended):

      • In your Sublime Text project, create a file named sublime-project if it doesn't exist.
      • Add the following configuration, replacing /path/to/your/venv/bin/python with the actual path to your virtual environment's Python.
      {
          "folders":
          [
              {
                  "path": "/path/to/your/project/folder"
              }
          ],
          "settings": {
              "LSP": {
                  "jedi": {
                      "python": "/path/to/your/venv/bin/python"
                  }
              }
          }
      }

      Save the file. Now, whenever you have this project open, LSP will use the Python interpreter from your virtual environment.


How to Use Jedi in Sublime Text (The Features)

Once configured, you can use powerful Jedi features. Most are triggered via the Command Palette or keybindings.

Feature How to Trigger What it Does
Autocompletion Tab or Ctrl+Space As you type, Jedi suggests variables, functions, classes, and keywords from your project and installed libraries.
Go to Definition F12 or Ctrl+Click on a symbol Jumps you directly to the line where a function, class, or variable is defined.
Go to Definition (in a new tab) Ctrl+K, Ctrl+B (default keybinding for "Find in Project" but can be remapped or accessed via Command Palette) Opens the definition in a new tab, which is useful for keeping your current context.
Find All References Shift+F12 Finds all the places in your codebase where a particular symbol (like a function name) is used.
Hover for Documentation Ctrl+K, Ctrl+D (default) or just Ctrl+Hover Shows you a pop-up with the docstring (documentation) for the function or class your cursor is on.
Symbols in Current File Ctrl+R Shows a list of all classes and functions in the current file, allowing you to quickly navigate to one.
Workspace Symbols Ctrl+Shift+R Searches for symbols (classes, functions, etc.) across your entire project.

Troubleshooting and Advanced Configuration

My autocompletion is slow or doesn't work.

  1. Check your Python path: This is the #1 issue. Ensure your python.executable setting correctly points to the Python interpreter of your virtual environment. If it points to a system Python without your project's packages, Jedi won't know about them.
  2. Disable other linters: If you have other linters like Pylint or Flake8 running via other packages, they can conflict with Jedi or slow things down. Try disabling them to see if performance improves.
  3. Check for syntax errors: Jedi can have trouble analyzing a file if it has major syntax errors. Fixing these can restore functionality.

I want to customize keybindings.

You can override default keybindings by adding them to your User Key Bindings file (Preferences > Key Bindings - User).

Example: To change "Go to Definition" to F2:

[
    { "keys": ["f2"], "command": "lsp_symbol_definition" }
]

You can find the command names by looking at the LSP package's documentation or by using the Ctrl+Shift+P menu to see available commands.

How do I see logs or debug?

If Jedi is misbehaving, the logs are your best friend.

  1. Open the Sublime Text console: View > Show Console or `Ctrl+`` (backtick).
  2. You can also find more detailed logs in the output panel. Open the Command Palette and run LSP: Show Log. This will show you communication between Sublime Text and the Jedi server, which is great for spotting errors.

Alternative (but not recommended) Method: SublimeJedi

You might find an old package called SublimeJedi. Do not use it.

  • It's unmaintained: It hasn't been updated in years and is incompatible with modern Sublime Text versions.
  • LSP is the standard: The LSP + Jedi Language Server combination is the modern, actively maintained, and far more robust way to get Jedi support in Sublime Text. It provides a consistent experience across all languages.
分享:
扫描分享到社交APP
上一篇
下一篇