Skip to content

Getting Started


Sourcery command line interface can be installed by running:

pip install sourcery-cli


Once installed you can interactively login with:

sourcery login

which will open up a browser tab and ask for confirmation. This is only needed once per computer.

Please be aware that the Sourcery CLI requires a Team subscription to use. You can upgrade here.

Refactoring & Analyzing Code with the CLI

To display suggested refactorings as a diff:

sourcery refactor {file_or_directory}

And to apply those changes to the files:

sourcery refactor --in-place {file_or_directory}

Running Sourcery in the Command Line

Only applying specific rules

To apply specific Sourcery refactoring rules, you can use the --include (or -i) option multiple times:

sourcery refactor --in-place -i use-fstring-for-concatenation -i use-named-expression {file_or_directory}

Note that this will override the skip refactorings configuration in your sourcery.yaml project and user configuration. It will respect the source code skip comments, as well as the python version you have configured.

Command Line Options

Sourcery provides a few options for running. You can list them by running sourcery refactor --help

sourcery refactor --help

Usage: sourcery refactor [OPTIONS] [SRC]...

  Refactor SRC files/directories. Reads from stdin when SRC is -

  -c, --code TEXT     Refactor the code passed in as a string.
  -i, --include TEXT  Only run the specified rule. This option can be used
                      multiple times.
  --check             Return exit code 1 if refactorings found.  [default:
  --diff / --no-diff  Output a diff for each file on stdout.  [default: diff]
  --in-place          Make changes to files in place  [default: False]
  --backup TEXT       Use with --in-place to backup changed files with given
  -h, --help          Show this message and exit.

You can also set up Sourcery to run as a Pre-Commit Hook or as part of your CI.

Configuring Sourcery

Sourcery reads configuration settings from .sourcery.yaml in the project directory. Full details are described here.

Back to top