[英]VS Code: Relative Import - No module named x
I know this is yet another question about "No module named x".我知道这是关于“没有名为 x 的模块”的另一个问题。 But I've tried everything I could find from stackoverflow (and also the internet) but it's still not working for me.
但是我已经尝试了从stackoverflow(以及互联网)中找到的所有东西,但它仍然不适合我。
Steps I create a project:我创建项目的步骤:
my_project
then cd my_project
my_project
的项目,然后cd my_project
python -m venv.env
and open a VS Code workspace with this folderpython -m venv.env
并使用此文件夹打开 VS Code 工作区my_project
├── __init__.py
├── folder_1
│ ├── file_1.py
│ └── folder_2
│ └── file_2.py -- this is where the function my_function is written
However, in file_1.py
when I try to import a function in folder_2/file_2.py
as below但是,在
file_1.py
中,当我尝试在folder_2/file_2.py
中导入 function 时,如下所示
# file_1.py
from folder_1.folder_2.file_2 import my_function
Then I bump into the following issue:然后我遇到了以下问题:
ModuleNotFoundError: No module named 'folder_1'
Hope someone can help.希望有人可以提供帮助。
UPDATE: CSBigSur is correct.更新: CSBigSur 是正确的。 In my case, it's just a problem with Pylance
就我而言,这只是Pylance的问题
Try this:尝试这个:
import folder2.file2 as f2
# your code
f2.your_func()
or或者
from folder2.file2 import your_func
# your code
your_func()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.