1

I am running Python 3.9.5 in interactive mode on VSCode. My first cell looks like this:

pip commands giving errors

The error is

Invalid character in token "" Pylance

.
The code still runs fine when I press Run Cell.

How can I make Pylance ignore these commands or get rid of the errors somehow?

Extensions:

  • Pylance 1021.5.1
  • Python 2021.5.829140558
  • Jupyter 2021.5.745244803

OS: Windows 10

gunar.kroeger
  • 167
  • 1
  • 11
  • This `!pip` command generally used in `Jupyter-notebook` so you go with `pip` or `pip3` as per your requirement – Bhavya Parikh May 11 '21 at 09:57
  • 1
    Yeah, the interactive python mode runs on a Jupyter server, but pylance still interprets the file as a normal python file. Thus it gives out squiggly lines even though the code runs fine. removing the exclamation mark or changing to pip3 doesn't solve the issue.. – gunar.kroeger May 11 '21 at 11:13
  • 1
    @gunar.kroeger -This "!pip" is not a standard sentence of python, so it is recognized as an irregular sentence by "Pylance" in the ".py" file. It is recommended that you use the command "pip install module_name" in the VS Code terminal to install the required module. Also, since it successfully installed the module, you could ignore this warning. – Jill Cheng May 12 '21 at 06:22

1 Answers1

2

This will be reported as a warning:

!pip install numpy

This will force Pylance to ignore warnings in that line

!pip install numpy # type: ignore

enter image description here

This is ok if you only have a few lines, but ideally, I would like to have a single comment/annotation to disable the linter and then another comment/annotation to re-enable it, but I can't figure out how to do that, perhaps something like:

# type: ignore on      <<<<<<<<<< NOTE: THIS DOES NOT WORK, I JUST WISH IT DID
!pip install numpy
!pip install matplotlib
!pip install pandas
# type: ignore off     <<<<<<<<<< NOTE: THIS DOES NOT WORK, I JUST WISH IT DID

MattG
  • 5,589
  • 5
  • 36
  • 52
  • 1
    Even though this solution removes the Pylance warning, it has the side effect of suppressing the output. Is there a way to still view the output of a magic command? For example, when using single and double question marks: `?func_name` and `??func_name`, the output should be details about the function. – datalifenyc Jul 29 '22 at 17:13