8

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?

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

6 Comments

This image isn't very clear, but updating to v1.63 on Windows 10 worked. Right-clicking -> Rename Symbol (all occurrences) works across files and import statements now.
@starball I don't get that prompt as in video. I have python and pylance installed
Neither did I. No prompt and it didn't do any refactoring. Is it something to do with what OS you're on? I'm using VS Code on Mac OS 14.2.1. 'refactor' doesn't even show up in the help search.
This is just beautiful, beautiful I say!
|
2

Some people have applied for this feature, but it looks a little bit difficult:

I actually don't like this idea because Python's import mechanism is much richer than TypeScript's so there's no guarantee of being able to get this right (i.e. TS is based entirely on file paths so updates are simple, Python's import system is a bit fancier 😉 ).

It's a neat idea, but implementing this correctly is extremely difficult without executing code.

You can refer to this page.

1 Comment

Thanks for linking to this discussion. Is there any advice for a novice Python developer trying to stay safe and productive while refactoring a (perceptively) large code base? Some clever Find and Replace with regex, perhaps?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.