Skip to content
Rupert Bedford edited this page Oct 16, 2018 · 12 revisions

Using pyls-black in Atom

  1. Install Atom and the ide-python package:

    brew cask install atom
    apm install atom-ide-ui
    apm install ide-python 
  2. Create a Python virtualenv and install pyls-black along with its dependencies:

    # Create a directory to keep your virtualenvs in
    mkdir -p ~/.virtualenvs
    
    # Create a new virtualenv
    python3 -m venv ~/.virtualenvs/ide-python
    
    # Install pyls-black in the newly created virtualenv
    ~/.virtualenvs/ide-python/bin/python3 -m pip install pyls-black

    Note: this will create a virtualenv in ~/.virtualenvs/ide-python but the exact location is up to you!

  3. Set the Python path in ide-python:

    Atom → Preferences → Packages → ide-python → Under Settings → Python Executable

    It should be set to the full path to the Python executable created in the previous step: /Users/rupert/.virtualenvs/ide-python/bin/python3 (replace /Users/rupert with the path to your home folder - ~ is not expanded).

    An alternative is to update your Atom config file directly:

    # config.cson
    
    "*":
      "ide-python":
        python: "/Users/rupert/.virtualenvs/ide-python/bin/python3"
  4. pyls-black should now be ready to use! When you open a Python file, the Python Language Server (pyls) will be started in the background. You can then format your code with black by:

    • Right-clicking in the editor and choosing Format Code from the context menu
    • Or using the ⌘⇧C keyboard shortcut
    • Or opening the command palette ⌘⇧P and choosing the Code Format - Format Code command

    atom-ide-ui can also be configured to format on save.

Configuring black

The pyls-black plugin will use your project's existing pyproject.toml file if it has one.

Format on Save

Atom → Preferences → Packages → atom-ide-ui → Under Code Formatting → Format on Save

Alternatively you can update your Atom config file directly:

# config.cson

"*":
  "atom-ide-ui":
    "atom-ide-code-format":
      formatOnSave: true

Help!

Most issues can be debugged using the logs but you'll need to enable them first:

  1. View → Developer → Toggle Developer Tools ⌥⌘I
  2. Click on the Console tab and type atom.config.set('core.debugLSP', true) to enable debugging
  3. Restart Atom ⌘Q for the config change to take effect
  4. Re-open the Developer Tools (see step 1)
  5. When you open a Python file you should see some log messages in the console
Clone this wiki locally