尝试导入导入另一个模块的模块,获取ImportError

时间:2022-05-27 13:12:56

In ajax.py, I have this import statement:

在ajax.py中,我有这个import语句:

import components.db_init as db

In components/db_init.py, I have this import statement:

在components / db_init.py中,我有这个import语句:

# import locals from ORM (Storm)
from storm.locals import *

And in components/storm/locals.py, it has this:

在components / storm / locals.py中,它有:

from storm.properties import Bool, Int, Float, RawStr, Chars, Unicode, Pickle
from storm.properties import List, Decimal, DateTime, Date, Time, Enum
from storm.properties import TimeDelta
from storm.references import Reference, ReferenceSet, Proxy
from storm.database import create_database
from storm.exceptions import StormError
from storm.store import Store, AutoReload
from storm.expr import Select, Insert, Update, Delete, Join, SQL
from storm.expr import Like, In, Asc, Desc, And, Or, Min, Max, Count, Not
from storm.info import ClassAlias
from storm.base import Storm

So, when I run that import statement in ajax.py, I get this error:

所以,当我在ajax.py中运行import语句时,我收到此错误:

<type 'exceptions.ImportError'>: No module named storm.properties

I can run components/db_init.py fine without any exceptions... so I have no idea what's up.

我可以运行components / db_init.py,没有任何异常...所以我不知道是什么。

Can someone shed some light on this problem?

有人能解释一下这个问题吗?

2 个解决方案

#1


I would guess that storm.locals' idea of its package name is different from what you think it is (most likely it thinks it's in components.storm.locals). You can check this by printing __name__ at the top of storm.locals, I believe. If you use imports which aren't relative to the current package, the package names have to match.

我猜想storm.locals关于它的包名的想法与你的想法不同(很可能它认为它在components.storm.locals中)。你可以通过在storm.locals的顶部打印__name__来检查这一点,我相信。如果使用与当前包不相关的导入,则包名必须匹配。

Using a relative import would probably work here. Since locals and properties are in the same package, inside storm.locals you should be able to just do

使用相对导入可能在这里工作。由于本地和属性在同一个包中,所以在storm.locals中你应该能够做到

from properties import Bool

and so on.

等等。

#2


You either need to

你要么需要

  • add (...)/components/storm to PYTHONPATH,
  • 向PYTHONPATH添加(...)/ components / storm,

  • use relative imports in components/storm/locals.py or
  • 在components / storm / locals.py或中使用相对导入

  • import properties instead of storm.properties
  • 导入属性而不是storm.properties

#1


I would guess that storm.locals' idea of its package name is different from what you think it is (most likely it thinks it's in components.storm.locals). You can check this by printing __name__ at the top of storm.locals, I believe. If you use imports which aren't relative to the current package, the package names have to match.

我猜想storm.locals关于它的包名的想法与你的想法不同(很可能它认为它在components.storm.locals中)。你可以通过在storm.locals的顶部打印__name__来检查这一点,我相信。如果使用与当前包不相关的导入,则包名必须匹配。

Using a relative import would probably work here. Since locals and properties are in the same package, inside storm.locals you should be able to just do

使用相对导入可能在这里工作。由于本地和属性在同一个包中,所以在storm.locals中你应该能够做到

from properties import Bool

and so on.

等等。

#2


You either need to

你要么需要

  • add (...)/components/storm to PYTHONPATH,
  • 向PYTHONPATH添加(...)/ components / storm,

  • use relative imports in components/storm/locals.py or
  • 在components / storm / locals.py或中使用相对导入

  • import properties instead of storm.properties
  • 导入属性而不是storm.properties