question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Specifc relative import statement causing issues with VSC highlighting and 'jumping' functionality.

See original GitHub issue

Environment data

  • VS Code version: 1.47.3 x64 , although also tested on 1.48.0 Insider
  • Extension version (available under the Extensions sidebar): v2020.7.96456
  • OS and version: Windows 10 Pro Verision 1909 64-bit
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.8.5 64-bit
  • Type of virtual environment used (N/A | venv | virtualenv | conda | …): N/A
  • Relevant/affected Python packages and their versions: N/A
  • Relevant/affected Python-related VS Code extensions and their versions: N/A
  • Value of the python.languageServer setting: Jedi

Steps to reproduce:

Example Directory: VSC_behaviour.zip

  1. Have the ‘Python’ extension installed.
  2. Download the Example Directory above, unzip, and open it with Visual Studio Code. You should arrive at this tree:
  3. This is the set-up for the testing for ‘Actual behaviour’ and ‘Expected behaviour’.
project
├── directory1
│   ├── __init__.py
│   ├── file2.py
│   └── file3.py
├── file1.py
├── run.py

Expected behaviour

I will describe the highlighting as ‘blue-highlighting’, as that’s what’s shown in my screenshots due to my theme, but presumably, there will be different colours for others.

1.1. Navigate to run.py. Assuming default keybindings, hover your mouse over the file1 part of import file1. It should highlight and underline in blue, shown below, and you should be able to left-click and ‘jump’ to file1. image3 1.2. Type file1. inside run.py, as if you were calling a function from file1. You should be auto-suggested the do_nothing() function, the behaviour of which I believe comes from the Python extension. image4

  1. Now navigate to file2.py. Notice the from . import file3. Hovering your mouse over file3 should result in highlighting/underlining/jumping (but it doesn’t - see Actual Behaviour). Moreover, writing file3. in file2.py should suggest the do_nothing() function.

  2. Replacing from . import file3 in file2.py with an alternative syntax should also have blue-highlighting/underlining/jumping/function suggestions. For instance, from .file3 import * should have this behaviour, as well as import file3, although the latter throws an editor error, but still has that highlighting/underlining and ‘jumping’.

Actual behaviour

Steps 1.1 and 1.2 show the expected behaviour (see images above). There is the expected blue-highlighting/underlining/jumping/function suggestions.

However, Step 2 does not show the expected behaviour. There is no blue-highlighting/underlining/jumping/function suggestions. image6 Perhaps this behaviour is due to my syntax, but my project appears to run fine with this step 2 syntax. This seems frustrating to me, as this syntax seems to be ‘legal’. Furthermore, if I manually write file3.do_nothing(), the function can run perfectly normally at execution, provided the function is ultimately called from run.py, the Python file that’s ran for the project. You can see an example of this expected behaviour here. You should see the text ‘I’m working!’ on terminal. VSC_Behaviour_run.zip

One possible theory I have is that the highlighting/etc. is being missed out due to the nature of the relative import. However, the highlighting/etc. work for another relative imports method, as shown in Step 3 below. Hence, I think that this specific relative import syntax could be causing conflicts with the Python extension for some reason, but I’m not sure why this might be occurring.

As mentioned above, the alternative import syntaxes in Step 3 do show the expected behaviour. There is highlighting/etc. image5 However, the reason I don’t the relative import from .file3 import * is because I’ve heard it’s generally considered bad practice. Furthermore, I cannot use import file3, as this does not work as a relative import; when run.py is ran, the import fails. I just showed it as an example. I’d like to still use the syntax used in Step 2 (from . import file3) if possible, as it fits my current project specifications the best.

Also, I noticed that disabling the Python extension stopped the highlighting/etc. for all imports, which made me think that the Python extension could be linked to this behaviour, rather than VSC itself.

Thank you!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:20 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
karthiknadigcommented, Aug 17, 2020

It is good to check with jedi. But I suspect it might be our usage of the Jedi API. We have plans on updating these and put Jedi behind LSP which should make use of the newer jedi APIs (see #11995 ). But that is a large change and will take some time.

0reactions
Hadatkocommented, Apr 28, 2021

Ok nevermind. Pylance has special option python.analysis.extraPaths which has to be used.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fix Python Relative Imports and Auto-completion in VSCode
In this video, you will learn how to properly handle Python relative imports without extending the sys.path. Additionally, you will learn ...
Read more >
User and Workspace Settings - Visual Studio Code
Settings editor with Workspace tab highlighted. All features of the Settings editor such as settings groups, search, and filtering behave the same for ......
Read more >
python - Pylint "unresolved import" error in Visual Studio Code
This issue was because of the Python interpreter. You need to select the interpreter in Visual Studio Code using Shift + Ctrl +...
Read more >
Modern IDEs are magic. Why are so many coders still using ...
Vim can do code completion, colored highlighting, auto-indent, inline doc, jump-to, search and replace, run a terminal, execute terminal ...
Read more >
Version History - Panic - Nova
Addressed an issue with certain text operations (like toggling comments) in ... after a function or class definition now highlight and fold as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found