[英]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:我在 VS Code 中的项目如下所示:
/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.在 VS Code 中,/myproject 的路径是... ofc ... 设置为项目。
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 .
但我想要的是:包之间的导入:例如 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()). VS Code 没有显示任何错误,自动完成所有内容(例如,当键入 sub1_mod1 时。...我获得了所有功能并且可以 select sub1_mod1_def1())。 But the moment i run the code i get ModuleNotFoundError: No module named 'sub1'
但是当我运行代码时,我得到ModuleNotFoundError: No module named 'sub1'
I also tried to work with all the VS Code/Pylance options:我还尝试使用所有 VS Code/Pylance 选项:
"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. TL;DNR:深入(当前)路径是没有问题的。 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).通常,当前文件夹的默认设置是项目文件夹(我们可以搜索“Terminal: Execute In File Dir”,发现没有勾选)。
However, it still gives error moduleNotFound even if the file dir is correct.但是,即使文件目录正确,它仍然会给出错误 moduleNotFound。 I submit the problem to github .
我将问题提交给github 。
Here is an interim solution, we can use code-runner extension and use Run Code to run python file:这是一个临时解决方案,我们可以使用code-runner扩展并使用Run Code来运行 python 文件:
It is the code-runner that runs normally makes me think it is a bug.正常运行的代码运行器让我认为这是一个错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.