I have a messages folder(package) with __init__.py
file and another module messages_en.py
inside it. In __init__.py
if I import messages_en
it works, but __import__
fails with "ImportError: No module named messages_en"
我有一个带有__init__.py文件的消息文件夹(包)和另一个模块messages_en.py。在__init__.py中如果我导入messages_en它可以工作,但是__import__失败并出现“ImportError:No module named messages_en”
import messages_en # it works
messages = __import__('messages_en') # it doesn't ?
I used to think 'import x' is just another way of saying __import__('x')
我以前认为'import x'只是说__import __('x')的另一种方式
6 个解决方案
#1
Adding the globals argument is sufficient for me:
添加globals参数对我来说已经足够了:
__import__('messages_en', globals=globals())
In fact, only __name__
is needed here:
实际上,这里只需要__name__:
__import__('messages_en', globals={"__name__": __name__})
#2
If it is a path problem, you should use the level
argument (from docs):
如果是路径问题,则应使用level参数(来自docs):
__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module
Level is used to determine whether to perform
absolute or relative imports. -1 is the original strategy of attempting
both absolute and relative imports, 0 is absolute, a positive number
is the number of parent directories to search relative to the current module.
#3
__import__
is an internal function called by import statement. In everyday coding you don't need (or want) to call __import__
__import__是import语句调用的内部函数。在日常编码中,您不需要(或想要)调用__import__
from python documentation:
来自python文档:
For example, the statement import spam
results in bytecode resembling the following code:
例如,语句导入垃圾邮件导致字节码类似于以下代码:
spam = __import__('spam', globals(), locals(), [], -1)
On the other hand, the statement from spam.ham import eggs, sausage as saus
results in
另一方面,来自spam.ham的声明导入鸡蛋,香肠作为香肠的结果
_temp = __import__('spam.ham', globals(), locals(), ['eggs', 'sausage'], -1)
eggs = _temp.eggs
saus = _temp.sausage
more info: http://docs.python.org/library/functions.html
更多信息:http://docs.python.org/library/functions.html
#4
Be sure to append the modules directory to your python path.
确保将modules目录附加到python路径。
Your path (the list of directories Python goes through to search for modules and files) is stored in the path attribute of the sys module. Since the path is a list you can use the append method to add new directories to the path.
您的路径(Python用于搜索模块和文件的目录列表)存储在sys模块的path属性中。由于路径是一个列表,因此您可以使用append方法将新目录添加到路径中。
For instance, to add the directory /home/me/mypy to the path:
例如,要将目录/ home / me / mypy添加到路径:
import sys
sys.path.append("/home/me/mypy")
#5
You could try this:
你可以试试这个:
messages == __import__('Foo.messages_en', fromlist=['messages_en'])
#6
You need to manually import the top package of your dynamic package path.
您需要手动导入动态包路径的*包。
For example in the beginning of the file i write:
例如,在我写的文件的开头:
import sites
then later in code this works for me:
然后在代码中,这对我有用:
target = 'some.dynamic.path'
my_module = __import__ ('sites.%s.fabfile' % target, fromlist=["sites.%s" % target])
#1
Adding the globals argument is sufficient for me:
添加globals参数对我来说已经足够了:
__import__('messages_en', globals=globals())
In fact, only __name__
is needed here:
实际上,这里只需要__name__:
__import__('messages_en', globals={"__name__": __name__})
#2
If it is a path problem, you should use the level
argument (from docs):
如果是路径问题,则应使用level参数(来自docs):
__import__(name, globals={}, locals={}, fromlist=[], level=-1) -> module
Level is used to determine whether to perform
absolute or relative imports. -1 is the original strategy of attempting
both absolute and relative imports, 0 is absolute, a positive number
is the number of parent directories to search relative to the current module.
#3
__import__
is an internal function called by import statement. In everyday coding you don't need (or want) to call __import__
__import__是import语句调用的内部函数。在日常编码中,您不需要(或想要)调用__import__
from python documentation:
来自python文档:
For example, the statement import spam
results in bytecode resembling the following code:
例如,语句导入垃圾邮件导致字节码类似于以下代码:
spam = __import__('spam', globals(), locals(), [], -1)
On the other hand, the statement from spam.ham import eggs, sausage as saus
results in
另一方面,来自spam.ham的声明导入鸡蛋,香肠作为香肠的结果
_temp = __import__('spam.ham', globals(), locals(), ['eggs', 'sausage'], -1)
eggs = _temp.eggs
saus = _temp.sausage
more info: http://docs.python.org/library/functions.html
更多信息:http://docs.python.org/library/functions.html
#4
Be sure to append the modules directory to your python path.
确保将modules目录附加到python路径。
Your path (the list of directories Python goes through to search for modules and files) is stored in the path attribute of the sys module. Since the path is a list you can use the append method to add new directories to the path.
您的路径(Python用于搜索模块和文件的目录列表)存储在sys模块的path属性中。由于路径是一个列表,因此您可以使用append方法将新目录添加到路径中。
For instance, to add the directory /home/me/mypy to the path:
例如,要将目录/ home / me / mypy添加到路径:
import sys
sys.path.append("/home/me/mypy")
#5
You could try this:
你可以试试这个:
messages == __import__('Foo.messages_en', fromlist=['messages_en'])
#6
You need to manually import the top package of your dynamic package path.
您需要手动导入动态包路径的*包。
For example in the beginning of the file i write:
例如,在我写的文件的开头:
import sites
then later in code this works for me:
然后在代码中,这对我有用:
target = 'some.dynamic.path'
my_module = __import__ ('sites.%s.fabfile' % target, fromlist=["sites.%s" % target])