简体   繁体   English

Python 2.7无法导入名称<class>

[英]Python 2.7 cannot import name <class>

I have a simple module (python 2.7) from which I'm trying to import a class, but I get an error: 我有一个简单的模块(python 2.7),试图从中导入一个类,但出现错误:

ImportError: cannot import name Engine

My code is: 我的代码是:

from components import Engine

and in components: 在组件中:

class Engine(object):
    pass

On the other hand 另一方面

from components import *

works fine 工作正常

What an I doing wrong? 我做错了什么? Any hints appreciated 任何提示表示赞赏

If you have this structure: 如果您具有以下结构:

.
├── components
│   ├── Engine.py
│   └── __init__.py
└── main.py

And for example main.py is: 例如main.py是:

from components import Engine
print 'Hi'

And Engine.py is: Engine.py是:

class Engine(object):
   pass

You mustn't find any problem with import statement. 您一定不会发现import语句有任何问题。 The code run fine: 代码运行良好:

> python main.py 
Hi

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

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