Python模块导入:在导入的modu中导入的模块呢

2024-04-25 08:11:30 发布

您现在位置:Python中文网/ 问答频道 /正文

很抱歉提出这个可能很幼稚的问题。我试着找doc做些实验,但我想确定是这样的:

如果,在文件中测试.py,我有:

import module1

我在控制台里这样做:

import test

我不会在控制台中导入模块1。你知道吗

如果我这么做:

from test import *

此外,模块1不会被导入控制台。你知道吗

对吗? 谢谢!你知道吗


Tags: 模块文件frompytestimportdocmodule1
2条回答

你的实验可以很容易地从外壳上进行:

╭─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'>
import test

这只会将名称test导入当前命名空间。test命名空间中的任何内容都可以作为test.whatever访问;特别是,module1可以作为test.module1访问,尽管您不应该使用它。你知道吗

from test import *

这会将所有不以下划线开头的内容从test的命名空间导入当前命名空间。由于module1test的名称空间中可用,因此这将导入名称module1。你知道吗

相关问题 更多 >