很抱歉提出了这个可能很幼稚的问题。我试图寻找医生并做一些实验,但我想确保情况是这样的:
如果,在test.py文件中,我有:
import module1我在控制台上这样做:
import test我不会在控制台中导入module1。
如果我这么做
from test import *另外,module1也不会导入到控制台中。
对吗?谢谢!
发布于 2013-09-04 22:07:34
import test这只会将名称test导入当前命名空间。test名称空间中的任何内容都可以作为test.whatever访问;特别是,module1可以作为test.module1使用,尽管您不应该使用它。
from test import *这会将所有不以下划线开头的内容从test的名称空间导入到当前的名称空间中。因为module1在test的名称空间中可用,所以它确实导入了名称module1。
发布于 2013-09-04 22:07:09
你的实验可以很容易地从外壳上进行:
╭─phillip@phillip-laptop ~ ‹ruby-1.9.3@global› ‹pandas›
╰─$ echo "import module1" > test.py
╭─phillip@phillip-laptop ~ ‹ruby-1.9.3@global› ‹pandas›
╰─$ touch module1.py
╭─phillip@phillip-laptop ~ ‹ruby-1.9.3@global› ‹pandas›
╰─$ py
Python 2.7.5 (default, May 17 2013, 07:55:04)
[GCC 4.8.0 20130502 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import test
>>> test.module1
<module 'module1' from 'module1.py'>
>>> from test import *
>>> module1
<module 'module1' from 'module1.py'>https://stackoverflow.com/questions/18624546
复制相似问题