问题描述
我是否必须取出文件名中的所有空格才能导入它,或者是否有某种方式告诉 import
那里是空格?
Do I have to take out all the spaces in the file name to import it, or is there some way of telling import
that there are spaces?
推荐答案
你应该从文件名中取出空格。因为文件名用作导入模块的标识符(即 foo.py
将导入为 foo
)和Python标识符不能包含空格, import
语句不支持。
You should take the spaces out of the filename. Because the filename is used as the identifier for imported modules (i.e. foo.py
will be imported as foo
) and Python identifiers can't have spaces, this isn't supported by the import
statement.
如果你真的出于某种原因需要这样做,你可以使用 __ import __
函数:
If you really need to do this for some reason, you can use the __import__
function:
foo_bar = __import__("foo bar")
这将导入 foo bar.py
as foo_bar
。这与 import
语句略有不同,您应该避免使用它。
This will import foo bar.py
as foo_bar
. This behaves a little bit different than the import
statement and you should avoid it.
这篇关于如何在python中导入文件中的空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!