[英]Unable to import from a own modul
I have running Anaconda and Python 3.8.3 64-bit on Windows 10.我在 Windows 10 上运行 Anaconda 和 Python 3.8.3 64 位。
I want to import an own program modul from an other file in an other folder .我想从其他文件夹中的其他文件导入自己的程序模块。
The import does not work.导入不起作用。 Terminal says ModuleNotFoundError: No module named终端说 ModuleNotFoundError: No module named
VS Code Screenshot VS 代码截图
What I tryed:我尝试了什么:
__init__.py
and .env
in every project folder.将文件: __init__.py
和.env
放在每个项目文件夹中。Thank you in advance for the help预先感谢您的帮助
Best regards Sepp最好的问候塞普
I create a project with the same folder structure as yours, like the following screenshot shows:我创建了一个与您的文件夹结构相同的项目,如下面的屏幕截图所示:
In launch.json, you should add在launch.json中,你应该添加
"cwd": "${workspaceFolder}",
when testb.py looks for the module, this setting makes it first to search in the current workspace folder, and the code sys.path.append("./")
let it turn to its parent directory, now testb.py is at the same level as foldera, so you can use import foldera.testa
without any errors.当 testb.py 查找模块时,此设置使其首先在当前工作区文件夹中搜索,代码sys.path.append("./")
让它转到其父目录,现在 testb.py 位于与 foldera 级别相同,因此您可以使用import foldera.testa
而不会出现任何错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.