简体   繁体   English

无法从自己的模块导入

[英]Unable to import from a own modul


this is my first question and I searched the whole internet for one day and couldn't find a solution. 这是我的第一个问题,我在整个互联网上搜索了一天,但找不到解决方案。
Hopefully someone can help me here. 希望有人能在这里帮助我。

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 .我想从其他文件夹中的其他文件导入自己的程序模块。

  • foldera contains a testa.py file foldera 包含一个 testa.py 文件
  • folderb contains a testb.py file. folderb 包含一个 testb.py 文件。 I want to import foldera.testa我想导入文件夹a.testa

The import does not work.导入不起作用。 Terminal says ModuleNotFoundError: No module named终端说 ModuleNotFoundError: No module named

VS Code Screenshot VS 代码截图

What I tryed:我尝试了什么:

  • put the file: __init__.py and .env in every project folder.将文件: __init__.py.env放在每个项目文件夹中。
  • Reinstalling both programs and deleted temp files重新安装两个程序和删除的临时文件
  • Looking folder .vscode into settings.json showed that the python.pythonPath is set correctly to python.exe在 settings.json 中查看文件夹 .vscode 表明 python.pythonPath 已正确设置为 python.exe
  • Typed in python.exe sys.path.append('\\\\path\\\\to\\\\whatever') and os.environ['PYTHONPATH'] = '\\\\path\\\\to\\\\whatever'输入 python.exe sys.path.append('\\\\path\\\\to\\\\whatever') 和 os.environ['PYTHONPATH'] = '\\\\path\\\\to\\\\whatever'

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.

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