python中各种import语句之间的区别 [英] difference between various import statements in python

查看:129
本文介绍了python中各种import语句之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import sys
from sys import argv

我在pydocs中读到了有关import语句的内容。它说它分两步执行。 (1)找到一个模块,并在必要时进行初始化; (2)在本地名称空间(发生import语句的作用域)中定义一个或多个名称。第一种形式(不含)为列表中的每个标识符重复这些步骤。带有from的表单执行步骤(1)一次,然后重复执行步骤(2)。

I read about about import statement in pydocs. It says it executes in two steps. (1)find a module, and initialize it if necessary; (2) define a name or names in the local namespace (of the scope where the import statement occurs). The first form (without from) repeats these steps for each identifier in the list. The form with from performs step (1) once, and then performs step (2) repeatedly.

这里我理解第一种情况 sys 模块将作为第1步的一部分进行初始化,然后它将作为第2步提供给本地命名空间。
但是第二次进口表格会发生什么?
sys 模块初始化为第一步,只有 argv sys的函数模块(NO OTHER函数)可用于本地名称空间。
因为当我使用第二种形式的import语句时,我无法调用 sys 模块的任何其他函数。所以只想具体澄清一下。好像 sys 模块已从import语句初始化,然后我们应该能够使用 sys 调用其他函数。但这不起作用。

Here I understood that in first case sys module will be initialized as part of 1st step and then it will be made available to local namespace as 2nd step. But what will happen in case of second import form? Will sys module be initialized as first step and only argv function of sys module(NO OTHER function) is made available to local namespace. Because I am not able to call any other functions of sys module when I am using second form of import statement. So just want to clarify on it specifically. As if sys module has been initialized in from import statement then we should be able to call other functions using sys. But that is not working.

#import sys
from sys import argv

script, input = argv

print "This was the input entered by command line", input

print sys.path # this is not working giving error name sys is not defined.

我怀疑来自import statement sys 模块没有被初始化,只有 argv 函数被初始化但是在这种情况下,从步骤执行步骤(1)一次是什么意思pydocs?(注意:我正在工作在python 2.7)

I am suspecting in case of from import statement sys module is not getting initalized, only argv function is getting initialized but in that case what does from step performs step(1) once mean pydocs?(Note: I am working on python 2.7)

推荐答案

导入语句始终初始化整个模块。这些模块存储在 sys.modules 字典

The import statement always initializes the whole module. The modules are stored in the sys.modules dictionary.

从sys import argv 使用 sys 模块在本地绑定,只有 argv 。您不能在模块中使用名称 sys ,因为您没有导入该名称。

When you use from sys import argv the sys module is not bound locally, only argv is. You cannot use the name sys in your module, because you didn't import that name.

您只能使用如果您单独导入 sys ,则到达 sys 模块:

You can only reach the sys module if you imported sys separately:

from sys import argv

script, input = argv

import sys
print sys.path

您可以通过访问 sys.modules 来访问所有导入的模块:

And you can always access all imported modules by accessing sys.modules:

from sys import modules

print modules['sys'].path

这里我将名称 modules 绑定到 sys.modules 字典,并通过该引用,找到 sys 模块,并引用路径属性。

Here I bound the name modules to the sys.modules dictionary, and through that reference, find the sys module, and reference the path attribute.

演示:

>>> from sys import modules
>>> modules['sys']
<module 'sys' (built-in)>
>>> sys
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'sys' is not defined

这篇关于python中各种import语句之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆