简体   繁体   English

Pycharm - ModuleNotFoundError

[英]Pycharm - ModuleNotFoundError

I'm trying to create my first package using the format from the python tutorial and am getting ModuleNotFoundError when trying to run unittests from the tests subfolder in Pycharm, but can run the same test fine through IDLE.我正在尝试使用python 教程中的格式创建我的第一个 package 并且在尝试从 ZE47D298C46B582C0F8D10F6930664F0 中的测试子文件夹运行单元测试时遇到 ModuleNotFoundError,但可以通过 ID5 运行相同的测试

As per the tutorial this is the general folder structure根据教程,这是一般的文件夹结构

packaging_tutorial包装教程

── LICENSE ── 执照

── README.md ── 自述文件.md

── example_pkg ── example_pkg

└── __init__.py └── __init__.py

── setup.py ── setup.py

── tests ── 测试

└── example_test.py └── example_test.py

Initially I couldn't get the test to run at all on either IDLE or Pycharm.最初,我根本无法在 IDLE 或 Pycharm 上运行测试。 After using pip to install the package through Command Prompt I've been able to run the tests through IDLE.通过命令提示符使用 pip 安装 package 后,我已经能够通过 IDLE 运行测试。 However when trying to run them in Pycharm, I'm getting ModuleNotFound.但是,当尝试在 Pycharm 中运行它们时,我得到了 ModuleNotFound。

I've tried using the suggestions from this previous question of Invalidate Cache/Restart, I've checked Project Interpreter & the confirmed the package is listed, I've tried doing pip install of the package in the Pycharm terminal. I've tried using the suggestions from this previous question of Invalidate Cache/Restart, I've checked Project Interpreter & the confirmed the package is listed, I've tried doing pip install of the package in the Pycharm terminal.

I'd imagine this is a relatively straightforward fix.我想这是一个相对简单的解决方法。 This is my first project using Pycharm, and I'm guessing there was/is some preliminary stuff that I could/should have done that would avoid this issue.这是我第一个使用 Pycharm 的项目,我猜我可以/应该做一些初步的事情来避免这个问题。

You need to add an empty __init__.py file in your tests folder to allow the test platform to successfully discover modules in outer directories.您需要在您的tests文件夹中添加一个空的__init__.py文件,以允许测试平台成功发现外部目录中的模块。

After some mucking about on the Pycharm and the Pycharm forum, I think I've come up with a solution.在对 Pycharm 和 Pycharm 论坛进行了一番讨论之后,我想我已经想出了一个解决方案。 Right-clicking the file name and selecting "Modify Run Condition", and reselecting the file path seems to have done the trick.右键单击文件名并选择“修改运行条件”,然后重新选择文件路径似乎已经成功了。 I'm guessing that because I moved the files from the folder they were created to a different one, that Pycharm was trying to maintain the original link.我猜是因为我将文件从它们创建的文件夹移动到另一个文件夹,所以 Pycharm 试图维护原始链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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