简体   繁体   English

运行时Python导入错误

[英]Python Import error at runtime

I have a project with the following package structure. 我有一个具有以下程序包结构的项目。

Trial
├── README.md
├── setup.py
└── trial
   ├── __init__.py
   ├── data
   │      └── cfg
   │         ├── project.properties
   │         ├── routes.json
   │         ├── springconfig.xml
   │         └── validators.xml
   ├── main.py
   └── taskqueue
       ├── __init__.py
       ├── manager.py
       ├── manager.pyc
       └── worker.py

Now I am using Pycharm IDE, which does check for faulty imports. 现在,我正在使用Pycharm IDE,它会检查错误的导入。 In the manager.py module I have an import 在manager.py模块中,我有一个导入

 from trial.taskqueue.worker import Worker

Now my IDE doesnt complain about any import error here whatsoever, but when I run my application from trial directory by executing 现在我的IDE不会在这里抱怨任何导入错误,但是当我通过执行从试用目录运行应用程序时

 python main.py run

I get an import error stating 我收到一个导入错误,指出

 from trial.taskqueue.worker import Worker
 ImportError: No module named trial.taskqueue.worker

Why is the import not working and is there any fault with the project structure? 为什么导入无法正常进行,并且项目结构有问题?

manager.py seems to be in the same directory as worker.py so importing it like the following manager.py似乎与worker.py处于同一目录,因此按如下所示进行导入

from worker import Worker

Should work for you 应该为你工作

Did you try: 你试过了吗:

    from taskqueue.worker import Worker

Because there is no "trial" dir, once you are working inside "trial" ! 因为没有“ trial”目录,所以一旦在“ trial”中进行工作!

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

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