从另一个文件导入类是否也导入该文件依赖项?

时间:2017-12-15 16:09:09

标签: python import dependencies

假设我有一个名为src.py的脚本,其中定义了几个函数;这些函数都在一个名为<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="span6"> <div class="portlet box blue checkbox-assigned"> <div class="portlet-title"> <div class="caption"><i class="icon-globe"></i>Jobs</div> </div> <label><div class="checker"><span class="checked"><input type="checkbox" name="colorCheckbox" value="76" checked=""></span></div> Fahon</label> <label><div class="checker"><span class="checked"><input type="checkbox" name="colorCheckbox" value="60" checked=""></span></div> Nangle</label> <label><div class="checker"><span class="checked"><input type="checkbox" name="colorCheckbox" value="Todo" checked=""></span></div> Todo</label> <label><div class="checker"><span class="checked"><input type="checkbox" name="colorCheckbox" value="Pending Us" checked=""></span></div> Pending Us</label> </div> <div style="height: 500px; overflow-y: scroll"> <div class=" 76 Todo"> <div class="dashboard-stat green jobsCard" data-assigned="Fahon" data-status="Todo"> <div> Shop Name: <strong style="padding-right: 3em">Pizza</strong> Job Name: <strong>Machine not working</strong> - <strong>Todo</strong> <a style="float: right; color: #000000;">Priority: <strong>Normal</strong></a> </div> <div> Created By: <strong>Nangle</strong> <a style="float: right; color: #000000;">Assigned To: <strong>Fahon</strong></a> </div> <div> Created On <strong>24/10/2017</strong> Last Updated: <strong>24/10/2017 11:14</strong> <a style="float: right; color: #000000;" href="index.php?/job/view/43"><strong>View</strong></a> </div> </div> <div class="containerr collapsed"> <div class="headerTab"></div> <div class="content dashboard-stat blue"> <p> message </p> </div> </div> </div> <div class=" 60 Todo"> <div class="dashboard-stat green jobsCard" data-assigned="Fahon" data-status="Todo"> <div> Shop Name: <strong style="padding-right: 3em">The Castle</strong> Job Name: <strong>Accounts</strong> - <strong>Todo</strong> <a style="float: right; color: #000000;">Priority: <strong>Normal</strong></a> </div> <div> Created By: <strong>Nangle</strong> <a style="float: right; color: #000000;">Assigned To: <strong>Nangle</strong></a> </div> <div> Created On <strong>24/10/2017</strong> Last Updated: <strong>24/10/2017 11:19</strong> <a style="float: right; color: #000000;" href="index.php?/job/view/44"><strong>View</strong></a> </div> </div> <div class="containerr collapsed"> <div class="headerTab"></div> <div class="content dashboard-stat blue"> <p> message </p> </div> </div> </div> <div class=" 76 Todo"> <div class="dashboard-stat green jobsCard" data-assigned="Fahon" data-status="Todo"> <div> Shop Name: <strong style="padding-right: 3em">Time</strong> Job Name: <strong>Marketing Visit</strong> - <strong>Todo</strong> <a style="float: right; color: #000000;">Priority: <strong>Normal</strong></a> </div> <div> Created By: <strong>Nangle</strong> <a style="float: right; color: #000000;">Assigned To: <strong>Fahon</strong></a> </div> <div> Created On <strong>25/10/2017</strong> Last Updated: <strong>25/10/2017 10:32</strong> <a style="float: right; color: #000000;" href="index.php?/job/view/51"><strong>View</strong></a> </div> </div> <div class="containerr collapsed"> <div class="headerTab"></div> <div class="content dashboard-stat blue"> <p> message </p> </div> </div> </div> </div> </div> </div>的类中。这些函数需要LDAnumpy等库,这些库在脚本顶部作为标准导入。

现在,在一个名为scipy的新单独脚本中,我导入了类main.py

LDA

如果我现在在新的from src import LDA 脚本中使用LDA类及其所有函数,那么函数都能正常工作,即使我没有导入{{1}中所需的依赖项}} 的。 这是如何工作的?我不应该在main.py src.py中导入numpyscipy等工作吗?

1 个答案:

答案 0 :(得分:1)

这里和类似的问题已在这里得到解答。请参阅thisthis,了解python import modulefrom module import class功能的重要性。

总之,import Xfrom X import class逐行执行目标文件,因此所有依赖项将自动加载到模块的命名空间中。因此,如果您实例化该类并运行函数/方法,则将在X的名称空间(一个奇特的字典)中查找它们,其中存在对依赖项的引用。导入后,from X import class会创建一个具有相同名称的class引用,因此可以无限制地使用它。但需要注意的是模块X没有引用,因此无法使用X.obj限定符访问对象。使用普通的import X语句,您可以访问X中定义的所有对象。

Side注意:导入对象的dir()函数和 dict 属性是查看加载对象的好方法。下面给出一个例子

#script test.py
import numpy as np

class Baz():
    def __init__(self, num):
        self.a = np.arange(num)
        self.b = np.ones(num)
        print self.a, self.b

    def foo(self):
        prod = np.dot(self.a, self.b)
        return prod

>>> import test
>>> dir(test)
['Baz', '__builtins__', '__doc__', '__file__', '__name__', '__package__', 'np']
>>> test.__dict__.keys()
['__builtins__', '__file__', 'Baz', '__package__', 'np', '__name__', '__doc__']
>>> test.__dict__['np']
<module 'numpy' from '/home/user/bin/python2.7/site-packages/numpy/__init__.pyc'>
>>> x = test.Baz(5)
[0 1 2 3 4] [ 1.  1.  1.  1.  1.]
>>> x.foo()
10.0

现在是from import语句

>>> from test import Baz
>>> dir(test)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'test' is not defined
>>> dir(Baz)
['__doc__', '__init__', '__module__', 'foo']
>>> Baz.__module__
'test'
>>> Baz.__dict__.keys()
['__module__', 'foo', '__doc__', '__init__']
>>> x = Baz(5)
[0 1 2 3 4] [ 1.  1.  1.  1.  1.]
>>> x.foo()
10.0
>>> Baz.__dict__['foo'].__globals__.keys()
['__builtins__', '__file__', 'Baz', '__package__', 'np', '__name__', '__doc__']

正如您所看到的,模块numpy已加载并在函数的__globals__属性中可用,该属性再次返回所有已加载模块的字典。因此,当运行foo()方法时,它会在命名空间字典中执行查找并找到numpy。