简体   繁体   中英

VS Code and Python import - relative/absolute path problem

I know, that there are already threads open that discuss this topic, but NONE helped so far.

My project in VS Code looks like this:

    /myproject    
        main.py
        /sub1
             __init__.py
             sub1_mod1.py
            /sub1_1
                 __init__.py
                 sub1_1_mod1.py
            /sub1_2
                 __init__.py
                 sub1_2_mod1.py
        /sub2
             __init__.py
             sub1_mod1.py
            /sub2_1
                 __init__.py
                 sub2_1_mod1.py
            /sub2_2
                 __init__.py
                 sub2_2_mod1.py

In VS Code the path of /myproject is... ofc... set as the project.

Code like

    # main.py absolute imports
    from sub1 import sub1_mod1 as sub1_mod1
    from sub1.sub1_1 import sub1_1_mod1 as sub1_1_mod1

    if __name__ == "__main__":
        sub1_mod1.sub1_mod1_def1()
        sub1_1_mod1.sub1_1_mod1_def1()

works like a charm. But what i want is: Imports between packages: eg import /sub1/sub1_mod1.py in /sub2/sub2_mod.py . I am using this code:

    # sub2_mod1.py
    import sub1.sub1_mod1 as sub1_mod1

    if __name__ == "__main__":
        sub1_mod1.sub1_mod1_def1()

VS Code shows no errors, auto completes everything (eg when typing sub1_mod1. ... i get all functions and can select sub1_mod1_def1()). But the moment i run the code i get ModuleNotFoundError: No module named 'sub1'

I also tried to work with all the VS Code/Pylance options:

   "python.analysis.importFormat": "relative" 
   - - - - - - - - - - - - OR - - - - - - - - - - - -
   "python.analysis.importFormat": "absolute"

   ...

   "python.analysis.autoImportCompletions": true
   - - - - - - - - - - - - OR - - - - - - - - - - - -
   "python.analysis.autoImportCompletions": false

   ...

       "python.analysis.extraPaths": [
        "/sub1",
        "/sub2",
        "/sub1/sub1_1",
        "/sub1/sub1_2",
        "/sub2/sub2_1",
        "/sub2/sub2_2"
    ]
    - - - - - - - - - - - - OR - - - - - - - - - - - -
    "python.analysis.extraPaths": []

None of the options changed a thing.

TL;DNR: Going deeper in in the (current) path is no problem. However, going in the other direction does not work. Any clues on how to set this up? Oh and I prefer a solution without import os and stay "native".

I create the similar directory structure and I get the same problem.

Usually, the default setting about the current folder it the project folder (We can search "Terminal: Execute In File Dir" and find it's not checked).

However, it still gives error moduleNotFound even if the file dir is correct. I submit the problem to github .

Here is an interim solution, we can use code-runner extension and use Run Code to run python file:

在此处输入图像描述

It is the code-runner that runs normally makes me think it is a bug.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM