Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename and Refactor a Python File in VS Code

On PyCharm you can right-click a file -> Refactor -> Rename. And it would rename that file and update all import statements in all files in the project.

In VS Code, while I can rename and refactor symbols by highlighting them -> F2, this only works for modules, classes, their members, and variables.

E.g. I have an utils/__init__.py with:

from utils.readers import CSVReader

utils/readers.py with:

class CSVReader:
   pass

And main.py with:

from utils import CSVReader
r = CSVReader()

I would like to, for example, rename utils/readers.py -> utils/local_readers.py and have VS Code auto-update utils/__init__.py with:

from utils.local_readers import CSVReader

Dozens of google search results point to Move TS (for TypeScript only). Is there a similar extension for Python, or some built-in hotkey I missed?

like image 680
Wassadamo Avatar asked Jun 28 '26 23:06

Wassadamo


1 Answers

Check out the enhanced module re-naming coming to vscode v1.63 (see release notes: python module rename refactoring.

Module rename refactoring

You can now more easily rename modules with the Python and Pylance extensions. Once you rename a Python module, you’ll be prompted to choose whether you’d like to change all import and references throughout your code. If you’re not sure, you can first preview what all the changes will look like before you make the decision. Once you’re confident about it, you can click on Apply Refactoring, or Discard Refactoring to not have them applied.

python module refactoring

like image 107
Mark Avatar answered Jul 01 '26 13:07

Mark