我知道有很多关于在python中导入模块的问题,但是我的问题似乎有些不同。在
我试图理解什么时候需要导入整个模块,而不是必须导入模块中的特定条目。看来这两种方法中只有一种有效。在
例如,如果我想使用basename,导入操作系统路径不会成功的。在>>> import os.path
>>> basename('scripts/cy.py')
Traceback (most recent call last):
File "", line 1, in
NameError: name 'basename' is not defined
相反,我需要从操作系统路径如
^{pr2}$
如果我想用shutil.copyfile文件,无法从shutil导入copyfile>>>
>>> from shutil import copyfile
>>>
>>> shutil.copyfile('emma','newemma')
Traceback (most recent call last):
File "", line 1, in
NameError: name 'shutil' is not defined
相反,我必须像>>>
>>> import shutil
>>>
>>> shutil.copyfile('emma','newemma')
'newemma'
>>>
我能做到这一点的唯一方法就是通过实验。
有没有一些避免实验的指南?在