Christopher Mauney
Christopher Mauney

Reputation: 509

Visual Studio Code quick-fix & python

Visual Studio Code is never able to populate the 'Quick Fix' contextual drop down, only displaying 'No Code Actions Available'

Python extension is installed, along with python3.7.3 and flake8, pep8.

Upvotes: 32

Views: 18449

Answers (9)

I've fixed this problem, when I added setting to "settings.json".

{
    "python.analysis.packageIndexDepths": 3,
    "python.analysis.autoImportCompletions": true,
    "python.analysis.indexing": true
}

VS Code uses Pylance (or another language server) for a code analysis. Sometimes VSCode cannot find imports like our problem, before the deep setting. You can fix it: just open settings.json and add json written above.

Upvotes: 0

Maxim Suslov
Maxim Suslov

Reputation: 5485

At least in VS Code 1.87.0 with installed Pylance v2024.2.2, it works in different ways, for example:

  • type path and it automatically suggests to use method from a package

auto popup

  • if you close popup or hover the cursor over the unknown work then run quick fix (CTRL+. on Windows/Linux or CMD+. on Mac or select it from the command palette):

quick fix

See also https://code.visualstudio.com/docs/python/editing#_quick-fixes

Upvotes: 2

Lorenz Walthert
Lorenz Walthert

Reputation: 4639

As others mentioned, this is now supported, but I could not get it work. I tried upgrading VS Code, reinstalling Python extension and Pylance, switching to pre-release, reloading VS Code, making Pylance the default language server for Python etc. What did the trick finally was looking at my JSON settings and get rid of settings (both user and workspace) that were no longer supported / conflicting and similar. Basically all the ones that were highlighted when you open the JSON in VS Code itself. Then, after re-loading VS Code, it magically worked: When I moved my curse into the object definition (e.g. the name of the class), the bulb showed up after a second or two.

Upvotes: 0

Greg7000
Greg7000

Reputation: 425

For import quick fixes, there is a solution.

Use the following VsCode Extension which work just as expected.

https://marketplace.visualstudio.com/items?itemName=Bar.python-import-helper

Upvotes: 0

Andy Brown
Andy Brown

Reputation: 5522

Having spent ages reading many articles on this, I've slightly improved my position. The right mouse button never comes up with anything, but I can press Ctrl + a full stop (period if you're in the US) to bring up suggested imports - sometimes.

I have Pylance installed in VS Code (so do you probably), which should be the default linter, but just in case I forced this in VS Code Settings:

// Defines type of the language server (should default to Pylance if installed anyway)
    "python.languageServer": "Pylance",
    

The comments are mine. But then I read the notes on PyLance, and it seems you have to enable this setting by adding these lines:

 // Offer auto-import completions.
    "python.analysis.autoImportCompletions": true,

Having said all of this, the feature has now stopped working again! I've left this in just in case a) it gives anyone any ideas or b) I get it working again and can come back to edit this answer.

Upvotes: 0

Pranav Kasetti
Pranav Kasetti

Reputation: 9935

I also recently tried the Sourcery VSCode Python Refactoring Extension to provide more refactorings that helps eliminate the errors in some cases implementing the line in question more clearly.

Upvotes: 1

albertgumi
albertgumi

Reputation: 342

Solution for 2021.

I got the same issue with Python for VSCode 1.54.2.

I solved it by installing the Pylance extension and making it the default Python language server (a message should pop up just after the installation asking if you want to make it the default Python language server). Now everything works flawlessly.

Upvotes: 5

Yukihiko Shinoda
Yukihiko Shinoda

Reputation: 975

Python extension started to support Quick Fix. First, function adding imports is supported.

Python in Visual Studio Code – November 2019 Release | Python

However

Python extension ver.2020.1.58038 and 2020.1.57204 have bug that it doesn't display Quick Fix.

Solution

Plan A: Use old version of Python extension

1.

If you haven't install Python extension, install it once.

2.

Install ver.2019.11.50794 or 2019.11.49689 by following steps in below answer.

vs code - rollback extension/install specific extension version - Stack Overflow

Plan B: Use Insiders version of Python extension

1.

Install Visual Studio Code - Insiders.

Download Visual Studio Code Insiders

2.

Install Python extension once, then click [Reload Required] button.

3.

Open Settings editor. (Ctrl + ,)

4.

Search by keyword: "Insiders Channel", then change pulldown to "daily" or "weekly", and save Settings. Then, Visual Studio Code start to download Insider version of Python extension. (Below status bar displays progress)

5.

When popup message "Please reload Visual Studio Code to use the insiders build of the Python extension." is displayed, click [Reload] button.

Requirement

Don't forget that there are two requirement to use Quick Fix feature.

  • Use Microsoft Python Language Server
  • Enable linting

Use Microsoft Python Language Server

Set python.jediEnabled to false in your settings.json file.

Enable linting

1.

Open Command Palette. (View > Command Palette... or F1 or Ctrl + Shift + P)

2.

Run "Python: Enable Linting" command.

3.

Select "On" in the drop-down menu.

cf. Editing Python Code in Visual Studio Code

Upvotes: 3

Brett Cannon
Brett Cannon

Reputation: 16100

The Python extension for VS Code currently doesn't offer any quick fixes.

Upvotes: 17

Related Questions