2

first of all, this is my workspace structure:

workspace_root
├── ...
├── libs
│   └── module_name
│       ├── README.md
│       ├── module_name
│       │   ├── __init__.py
│       │   ├── caller_with_main_calls_service.py
│       │   ├── independent.py
│       │   └── service_calls_indendent.py
│       └── setup.py
├── ...

my issue?

In VSCode, I can't navigate within a module's elements definition. That includes jumping from the import statement straight to the definition of it. I even couldn't start or debug the python main within caller_with_main_calls_service.py, because it said module not found. When installing the module via pip, it works. However, that's not desired during development...

What I tried so far:

  • tried .env approach and set PYTHONPATH to my interpreter + ${workspaceRoot}+"/libs/module_name/module_name"

  • set the workspace settings / settings.json like this:

{
   "python.linting.pylintEnabled": true,
   "python.linting.enabled": true,
   "python.pythonPath": "/path/to/conda_env/python",
   "python.autoComplete.extraPaths": [
       "${workspaceRoot}/libs/module_name/module_name"
   ],
   "editor.formatOnSave": true
}
  • before starting VSCode, I exported the $PYTHONPATH env var in the same manner as in 1) to include the lib path

What does the python files from the module look like:

# example: caller_with_main_calls_service.py
from module_name import service_calls_indendent as sci

Not sure, if that's relevant, but I am working with WSL.

3 Answers 3

1

[This is assuming you're using Python 3]

Navigation isn't working because the Python extension is looking in your workspace directory and any place specified in PYTHONPATH. So in this instance your import doesn't make sense to the extension because there is no module or package named module_name under workspace_root or workspace_root/libs. And because you specified an absolute import Python is only looking for module or packages with the name specified and not looking next to where you did the import.

I'm not quite sure what code/directory structure you're after, but either change your import to from . import service_calls_indendent or open workspace_root/libs/module_name as your workspace instead.

Sign up to request clarification or add additional context in comments.

3 Comments

already tried your suggestion before. with that, the navigations within vscode is working. however if I want to run caller_with_main_calls_service.py, which imports the own libraries, I get the following error: ``` Exception has occurred: ImportError attempted relative import with no known parent package File "/home/mf/code/caller_with_main_calls_service", line 2, in <module> from . import log_step_orchestrator as lso ```
@mchlfchr how are you executing it?
Assuming that you mean by "it" the application, I'm loading the whole root project into VSCode. I just used the default lauch configuration on the file caller_with_main_calls_service.py. As you may have seen, I solved it (see within my answer).
0

As commented, it's not enough to put from . import service_calls_independent aka relative imports.

Additionally, the launch.json needs to be adapted:

    {
      "name": "Python: Module Runner",
      "type": "python",
      "request": "launch",
      "module": "libs.module_name.caller_with_main_calls_service",
      "args": [
            1,
            2,
            3
      ],
      "env": {
        "FOO": "BAR"
      }
    }

The important part is the module, where you specify the file with the main inside - without any file extension.

Comments

-1

Please install below extensions it will autmatically configure vscode and you can easily find the defincations, source and also it will suggest the optimised way to code

https://marketplace.visualstudio.com/items?itemName=njpwerner.autodocstring

https://marketplace.visualstudio.com/items?itemName=trixnz.go-to-method

https://marketplace.visualstudio.com/items?itemName=ms-python.python

1 Comment

I have the python extension installed and not sure why I need your other extensions for that... i am basically able to debug and run python, so that isn't really related to the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.