Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python refactoring fails in Visual Studio Code

I switched to Visual Studio Code for Python programming recently. Below is my Python configuration in Visual Studio Code settings:

"python.pythonPath": "/Users/hzhang/.virtualenvs/env-2.7/bin/python",
"python.autoComplete.extraPaths": [
    "/Users/hzhang/Work/xxx/shared_modules"
],

Basically, I just configure the Python interpreter and add one extra shared module path.

When I try to refactor a variable name, it throws this error which says rope is not installed, and it doesn't work even I install it. Based on my understanding, refactor variables is a feature of Visual Studio Code, and it shouldn't rely on any specific language.

How can I fix this problem?

Enter image description here

Once I installed rope, refactor was still not working. It popups this error:

Enter image description here

I am on Python 2.7
Visual Studio Code: Version 1.19.3 (1.19.3)
Rope version: 0.10.7
like image 774
Haifeng Zhang Avatar asked Jan 26 '18 17:01

Haifeng Zhang


People also ask

Does VS Code support refactoring?

Source code refactoring can improve the quality and maintainability of your project by restructuring your code while not modifying the runtime behavior. Visual Studio Code supports refactoring operations (refactorings) such as Extract Method and Extract Variable to improve your code base from within your editor.

How does Visual Studio Code fix import error in Python?

To solve unresolved import error in Python, set your Python path in your workspace settings. If you are working with Visual Studio Code and import any library, you will face this error: “unresolved import”. Then reload the VSCode, and it will fix that error.


1 Answers

Renaming of variables is not a native Visual Studio Code feature for languages other than JavaScript and TypeScript.

It is specific to each language, and functionality is provided by separate extensions, specific to each language. The Python extension you have installed, uses the Rope library to perform refactoring/renaming of python variables and the like. So yes, you'll need to install it by closing in the Install rope button.

If you don't have the Install rope button, you can just go to cmd and type pip install rope. That should do the job as well.

If it doesn't work even after installing it, please could you file an issue on the Python extension GitHub repository.

like image 82
Don Avatar answered Nov 14 '22 10:11

Don