PyCharm 2022.2 Help

Rename refactorings

Use the Rename refactoring to change names of symbols, files, and all the references to them throughout code.

Renaming local variables or private methods can be done easily inline since only the limited scope is affected. Renaming classes or public methods could potentially impact a lot of files. Preview potential changes before you refactor.

Rename code in place

  1. In the editor start renaming a code element. PyCharm will display the Rename the usages of icon in the gutter.

  2. Click the gutter icon or press Alt+Enter and apply a suggestion.

    Rename inline

    You might be prompted to confirm changes in comments.

    Applying rename refactoring to comments

    Once you confirm your choice, PyCharm renames the code element and updates its usages accordingly.

    Rename refactoring result

Rename a code element

  1. In the editor, select an element you want to rename. If you need to rename a file, select one in the Project tool window.

  2. Press Shift+F6 or from the main menu, select Refactor | Rename.

  3. You can perform the rename refactoring in-place or press Shift+F6 again to open the Rename dialog. Enter a new name of the element to enable the Preview and Refactor buttons.

    The Rename dialog

    You can specify additional options. For example, specify where to search for element occurrences, or what else to rename. You can also specify a scope for the refactoring.

  4. Click Preview to see the potential changes or click Refactor.

    When you click Preview, PyCharm opens the Find tool window with the results of found usages where you can check the results and confirm the refactoring (Do Refactor).

Next time you invoke the Rename refactoring, PyCharm remembers the options you have specified inside the Rename dialog.

Examples

Let's rename a method:

Before

After

import datetime def was_published_today(self): return self.pub_date.date () == datetime.date.today()
import datetime def published_today(self): return self.pub_date.date () == datetime.date.today()
Last modified: 21 July 2022