6

I am trying to solve partial differential equations with Python using FEniCS. I installed it with anaconda and conda-forge and to use it, I activate the fenicsproject environment

source activate fenicsproject

I run my scripts in jupyter (that works), but often it is more convenient to use VS Code for more elaborate code. When I run the scripts written in VS Code in the (built-in) terminal, they run without error as long as I have the fenicsproject environment enabled.

But in the editor I get a lot of errors like this

[pylint] Unable to import '...' [E0401]'
[pylint] Undefined variable '...' [E0602]

How can I get rid of those errors in the editor, so that the real errors can stand out. What would be even better, make it that auto-complete and suggestions work for the packages like fenics, mshr etc.

jost21
  • 1,084
  • 3
  • 15
  • 29
  • Possible duplicate of [Visual Studio Code pylint: Unable to import 'protorpc'](https://stackoverflow.com/questions/43574995/visual-studio-code-pylint-unable-to-import-protorpc) – jrc Mar 21 '19 at 15:30

2 Answers2

6

According to the Python in Visual Studio Code docs, this is probably due to Visual Studio Code pointing at the wrong Python version.

1. Unable to import (pylint)

  • Scenario: You have a module installed, however the linter in the IDE is complaining about; not being able to import the module, hence error messages such as the following are displayed as linter errors:

    .. unable to import 'xxx' ..
    
  • Cause: The Python extension is most likely using the wrong version of Pylint.
Solution 1: (configure workspace settings to point to fully qualified python executable):
  1. Open the workspace settings (settings.json)
  2. Identify the fully qualified path to the python executable (this could even be a virtual environment)
  3. Ensure Pylint is installed for the above python environment
  4. Configure the setting "pythonPath" to point to (previously identified) the fully qualified python executable.

    "python.pythonPath": "/users/xxx/bin/python" ```
    
Solution 2: (open VS Code from an activated virtual environment):
  1. Open the terminal window
  2. Activate the relevant python virtual environment
  3. Ensure Pylint is installed within this virtual environment

    pip install pylint
    
  4. Close all instances of VS Code
  5. Launch VS Code from within this terminal window
    (this will ensure the VS Code process will inherit all of the Virtual Env environment settings)
wizzwizz4
  • 6,140
  • 2
  • 26
  • 62
  • That works. I tried both solutions, both of them resolve the initial problem (after installing `pylint` in the environment, i.e. `conda install pylint`). For solution 1: I activated the environment, got the path with `which python` and added it to the workspace settings. – jost21 Dec 08 '18 at 12:09
  • However, it does not get rid of all warnings/errors, more precise I get different ones: warning: `[pylint] Unused import ... from wildcart import [W0614]` I could get rid of this one by adding `# pylint: disable=unused-import` https://stackoverflow.com/questions/11957106/unused-import-warning-and-pylint (but only for solution 2 !?!) and error: `[pylint] Instance of ... has no ... member [E1101]` even though the method/property exists – jost21 Dec 08 '18 at 12:18
  • That's an actual problem with your code. :-) Don't use wildcard imports; they're horrible. – wizzwizz4 Dec 08 '18 at 12:23
  • Okay :) will change that. But the errors with `Instance of ... has no ... member` are on packages with non-wildcard imports, e.g. `from matplotlib import cm` – jost21 Dec 08 '18 at 12:34
  • @josh21 Hmm... That's probably something it should be able to deal with, but `pylint` would have no clue with C stuff (where there's no Python source available). Is that the cause? – wizzwizz4 Dec 08 '18 at 12:58
  • 1
    I just added `generated-members=cm.*` to `.pylintrc` https://stackoverflow.com/questions/33961756/disabling-pylint-no-member-e1101-error-for-specific-libraries not sure if that is good solution, but for now I have at least no errors showing – jost21 Dec 08 '18 at 12:59
  • @josh21 Did it fix it? – wizzwizz4 Dec 08 '18 at 13:00
  • Don't think it is a proper "fix" (since the member is still not recognized), but at least there are no errors anymore and I can focus on the actual work – jost21 Dec 08 '18 at 13:04
3

A slight correction to "Solution 1" above: use

"python.defaultInterpreterPath": "/users/xxx/bin/python" ```

source: https://github.com/microsoft/vscode-python/wiki/Setting-descriptions#pythondefaultinterpreterpath

Federico
  • 31
  • 2