Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyCharm: How to effectively rename a variable in all places where it occurs in a module at the same time?

Tags:

rename

pycharm

Given the following Python code

a = 3 # only this variable is going to be renamed by hand

A = 3  
abc = 3 
a += 1  

I want to rename all occurrences of the variable "a" to "b" automatically so that the result looks like this

b = 3 # variable was renamed from a to b manually

A = 3   # variable was not affected by the renaming
abc = 3 # variable was not affected by the renaming
b += 1  # variable was renamed from a to b automatically

Notice that I just want to rename the variables in the current module, nowhere else!

At the moment, to do this I am doing the following steps (Pycharm 2018.3.3 (Community Edition)):

  1. Place the cursor on the variable I want to rename.
  2. Open the renaming dialog via Shift+F6 and make sure that the checkbox
    "Search in comments and strings" is unchecked (see image of rename dialog).
  3. Type in the new name of the variable.
  4. Press Enter to refactor.

rename dialog

The problems with this method are:

  1. The "Looking for Usages" process (see image below) takes like forever for some variable names (renaming "a" to "b" takes about 27 seconds!!!). Looking for Usages porcess
  2. Occurences of the variable in comments are not renamed.

With this being said, my final question is: Is there a way to rename variables that is fast regardless of the variable name and only takes place in the current module?

like image 771
zwithouta Avatar asked Dec 06 '25 06:12

zwithouta


1 Answers

You can edit it using the refactor tool in Pycharm. Click on the variable and press shift+F6.

Rafactor dialog box screenshot

Type the name and press Enter.

like image 58
codergallery Avatar answered Dec 11 '25 15:12

codergallery



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!