Python LSP Server

1.14.0 · active · verified Sun Apr 12

Python LSP Server (`pylsp`) provides a Language Server Protocol (LSP) implementation for Python. It offers features like autocompletion, linting, formatting, refactoring, and more, integrating with various editors and IDEs. It is currently at version 1.14.0 and maintains an active release cadence with frequent minor updates and occasional major versions introducing new features or breaking changes.

Warnings

Install

Imports

Quickstart

The Python LSP Server is commonly launched by an editor/IDE as a subprocess. This quickstart demonstrates how to programmatically start the server, which then listens on stdin/stdout for Language Server Protocol messages. In a real application, an LSP client would handle sending and receiving these JSON-RPC messages.

import subprocess
import sys
import os
import time
import shutil

# The Python LSP Server is primarily meant to be run as a separate process
# and communicated with via stdin/stdout using the Language Server Protocol.
# This example shows how to launch it programmatically for testing or custom integration.

print("Launching python-lsp-server in a subprocess...")

try:
    # Using `sys.executable -m pylsp` ensures the correct Python environment is used.
    # We redirect stdout/stderr to pipes to prevent it from blocking the console.
    # For real use, stdout would carry LSP messages to a client.
    process = subprocess.Popen(
        [sys.executable, '-m', 'pylsp', '--log-level', 'WARNING'], # Adjust log-level as needed
        stdin=subprocess.PIPE, # Server expects LSP messages here
        stdout=subprocess.PIPE, # Server sends LSP messages here
        stderr=subprocess.PIPE, # Server logs errors/debug info here
        text=True, # Handle stdin/stdout as text
        bufsize=0 # Unbuffered I/O for real-time LSP communication
    )

    print("Server process started.")
    print("It is now listening for LSP messages on stdin and sending responses on stdout.")
    print("This process will block waiting for input unless a client sends messages.")
    print("Waiting for 2 seconds to simulate runtime...")
    time.sleep(2)

    # In a real scenario, you would send JSON-RPC LSP messages here, e.g.,
    # process.stdin.write('Content-Length: ...\r\n\r\n{"jsonrpc": "2.0", ...}\r\n')
    # process.stdin.flush()

    print("Attempting to terminate the server.")
    process.terminate() # Send SIGTERM (or equivalent on Windows)
    stdout, stderr = process.communicate(timeout=5) # Wait for it to exit and get output

    print(f"\nServer exit code: {process.returncode}")
    if stdout:
        print("\n--- Server STDOUT (LSP messages) ---")
        print(stdout.strip())
    if stderr:
        print("\n--- Server STDERR (Logs/Errors) ---")
        print(stderr.strip())

except FileNotFoundError:
    print(f"Error: `pylsp` command or `sys.executable` not found. Is python-lsp-server installed?")
except subprocess.TimeoutExpired:
    print("Server did not terminate gracefully within the timeout. Killing it.")
    process.kill()
    stdout, stderr = process.communicate()
except Exception as e:
    print(f"An error occurred: {e}")
finally:
    print("Quickstart demonstration complete.")

view raw JSON →