[root@fuel ~]# vi /var/lib/docker/devicemapper/mnt/4da57a0078c9d3f32e819373b67de41da37c34a27ee03f74016427e0223df5f2/rootfs/usr/lib/python2.6/site-packages/nailgun/db/sqlalchemy/models/node.py
     88
     89 class Node(Base):

304     @classmethod
    305     def delete_by_ids(cls, ids):
    306         db.query(Node).filter(Node.id.in_(ids)).delete('fetch')

32 from nailgun.db import db
可看出引用了/usr/lib/python2.6/site-packages/nailgun/db.py中的db变量。

发现db.py不存在:
[root@fuel ~]# ls -l /var/lib/docker/devicemapper/mnt/4da57a0078c9d3f32e819373b67de41da37c34a27ee03f74016427e0223df5f2/rootfs/usr/lib/python2.6/site-packages/nailgun/db.py

进入nailgun目录:
[root@fuel ~]# cd /var/lib/docker/devicemapper/mnt/4da57a0078c9d3f32e819373b67de41da37c34a27ee03f74016427e0223df5f2/rootfs/usr/lib/python2.6/site-packages/nailgun
[root@fuel nailgun]# ls
发现nailgun目录下还有一个db目录:
[root@fuel nailgun]# cd db
[root@fuel db]# ls
deadlock_detector.py   deadlock_detector.pyo  __init__.pyc  migration
deadlock_detector.pyc  __init__.py            __init__.pyo  sqlalchemy
从网络了解到:“__init__.py”会在被导入时执行,因此这是Python另一种导入方式。

[root@fuel db]# vi __init__.py
     17 from nailgun.db.sqlalchemy import db
可看出引用了/usr/lib/python2.6/site-packages/nailgun/db/sqlalchemy.py中的db变量。
同样不存在sqlalchemy.py,但存在sqlalchemy目录:

[root@fuel sqlalchemy]# vi __init__.py
     80 db = scoped_session(
     81     sessionmaker(
     82         autoflush=True,
     83         autocommit=False,
     84         bind=engine,
     85         query_cls=query_class,
     86         class_=session_class
     87     )
     88 )

24 from sqlalchemy.orm import scoped_session
     26 from sqlalchemy.orm import sessionmaker

可看出引用了/usr/lib/python2.6/site-packages/sqlalchemy/orm.py中的scoped_session、sessionmaker变量。不存在orm.py,且不存在orm目录。

19 from sqlalchemy import create_engine
     31 from nailgun.db.sqlalchemy import utils
     35 db_str = utils.make_dsn(**settings.DATABASE)
     36 engine = create_engine(db_str, client_encoding='utf8')
可看出引用了/usr/lib/python2.6/site-packages/nailgun/db/sqlalchemy.py中的utils对象或/usr/lib/python2.6/site-packages/nailgun/db/sqlalchemy/__init__.py中引用了utils对象。
db_str明显是数据库连接字符串,内容是“postgresql://nailgun:dvbbYDuU@168.5.21.2:5432/nailgun”。

72 if settings.DEVELOPMENT:
     73     query_class = DeadlockDetectingQuery
     74     session_class = DeadlockDetectingSession
     75 else:
     76     query_class = NoCacheQuery
     77     session_class = Session

32 from nailgun.settings import settings

进入docker:
[root@fuel sqlalchemy]# docker exec -it fuel-core-6.1-nailgun /bin/sh
sh-4.1# cd /usr/lib/python2.6/site-packages/nailgun/db/sqlalchemy
sh-4.1# vi __init__.py
     33 #zzy added
     34 from nailgun.logger import logger
     35 import sys

82 #zzy added
     83 logger.warning('____ffff____')
     84 logger.warning(sys.path)

sh-4.1# python __init__.py
sh-4.1# tail -n 20 /tmp/nailgun.log |grep WARNING
2016-01-27 06:24:20.257 WARNING [7f92c5287700] (__init__) ____ffff____
2016-01-27 06:24:20.257 WARNING [7f92c5287700] (__init__) ['/usr/lib/python2.6/site-packages/nailgun/db/sqlalchemy', '/usr/lib64/python26.zip', '/usr/lib64/python2.6', '/usr/lib64/python2.6/plat-linux2', '/usr/lib64/python2.6/lib-tk', '/usr/lib64/python2.6/lib-old', '/usr/lib64/python2.6/lib-dynload', '/usr/lib64/python2.6/site-packages', '/usr/lib/python2.6/site-packages']

orm.py或orm目录使用以下命令查找:
ls /usr/lib/python2.6/site-packages/nailgun/db/sqlalchemy/sqlalchemy/orm*
ls /usr/lib64/python26.zip/sqlalchemy/orm*
ls /usr/lib64/python2.6/sqlalchemy/orm*
ls /usr/lib64/python2.6/plat-linux2/sqlalchemy/orm*
ls /usr/lib64/python2.6/lib-tk/sqlalchemy/orm*
ls /usr/lib64/python2.6/lib-old/sqlalchemy/orm*
ls /usr/lib64/python2.6/lib-dynload/sqlalchemy/orm*
ls /usr/lib64/python2.6/site-packages/sqlalchemy/orm*
ls /usr/lib/python2.6/site-packages/sqlalchemy/orm*

最终发现:
sh-4.1# cd /usr/lib64/python2.6/site-packages/sqlalchemy/orm
sh-4.1# vi __init__.py
     63 from .scoping import (
     64     scoped_session
     65 )
     56 from .session import (
     57     Session,
     58     object_session,
     59     sessionmaker,
     60     make_transient,
     61     make_transient_to_detached
     62 )

从网络了解到,“.scoping”指“__init__.py”同一级目录的“scoping.py”:
sh-4.1# vi scoping.py
     14 __all__ = ['scoped_session']
     15
     16
     17 class scoped_session(object):
     18     """Provides scoped management of :class:`.Session` objects.
“__all__”定义只导出scoped_session类(当scoping.py被import的时候)。

sh-4.1# vi session.py
     27 __all__ = ['Session', 'SessionTransaction',
     28            'SessionExtension', 'sessionmaker']

2253 class sessionmaker(_SessionClassMethods):
   2254
   2255     """A configurable :class:`.Session` factory.
   2256
   2257     The :class:`.sessionmaker` factory generates new
   2258     :class:`.Session` objects when called, creating them given
   2259     the configurational arguments established here.

/usr/lib64/python2.6/site-packages/sqlalchemy/orm/scoping.py
    105     def query_property(self, query_cls=None):
    106         """return a class property which produces a :class:`.Query` object
    107         against the class and the current :class:`.Session` when called.
    108
    109         e.g.::
    110
    111             Session = scoped_session(sessionmaker())
    112
    113             class MyClass(object):
    114                 query = Session.query_property()
    115
    116             # after mappers are defined
    117             result = MyClass.query.filter(MyClass.name=='foo').all()
    118
    119         Produces instances of the session's configured query class by
    120         default.  To override and use a custom implementation, provide
    121         a ``query_cls`` callable.  The callable will be invoked with
    122         the class's mapper as a positional argument and a session
    123         keyword argument.
    124
    125         There is no limit to the number of query properties placed on
    126         a class.
    127
    128         """
    129         class query(object):
    130             def __get__(s, instance, owner):
    131                 try:
    132                     mapper = class_mapper(owner)
    133                     if mapper:
    134                         if query_cls:
    135                             # custom query class
    136                             return query_cls(mapper, session=self.registry()        )
    137                         else:
    138                             # session's configured query class
    139                             return self.registry().query(mapper)
    140                 except orm_exc.UnmappedClassError:
    141                     return None
    142         return query()
    143

Python的import嵌套的更多相关文章

  1. python之import机制

    1. 标准 import        Python 中所有加载到内存的模块都放在 sys.modules .当 import 一个模块时首先会在这个列表中查找是否已经加载了此模块,如果加载了则只是将 ...

  2. 浅析python 的import 模块(转)

    摘要: 学习python有几天了,对import一直不是很清楚,和C里面的include是否一样,重复引入有问题么?搜索路径是怎样的?整理解决下我的疑问. 一 模块的搜索路径 模块的搜索路径都放在了s ...

  3. python基础—函数嵌套与闭包

    python基础-函数嵌套与闭包 1.名称空间与作用域 1 名称空间分为: 1 内置名称空间   内置在解释器中的名称 2 全局名称空间   顶头写的名称 3 局部名称空间 2 找一个名称的查找顺序: ...

  4. python中的嵌套类

    python中的嵌套类 在.NET和JAVA语言中看到过嵌套类的实现,作为外部类一个局部工具还是很有用的,今天在python也看到了很不错支持一下.动态语言中很好的嵌套类的实现,应该说嵌套类解决设计问 ...

  5. python中import和from...import区别

    在python用import或者from...import来导入相应的模块.模块其实就是一些函数和类的集合文件,它能实现一些相应的功能,当我们需要使用这些功能的时候,直接把相应的模块导入到我们的程序中 ...

  6. Python中import的使用

    python中的import语句是用来导入模块的,在python模块库中有着大量的模块可供使用,要想使用这些文件需要用import语句把指定模块导入到当前程序中. import语句的作用 import ...

  7. python 的import机制2

    http://blog.csdn.net/sirodeng/article/details/17095591   python 的import机制,以备忘: python中,每个py文件被称之为模块, ...

  8. python中import和from...import...的区别

    python中import和from...import...的区别: 只用import时,如import xx,引入的xx是模块名,而不是模块内具体的类.函数.变量等成员,使用该模块的成员时需写成xx ...

  9. python的import与from...import的不同之处

    在python用import或者from...import来导入相应的模块.模块其实就是一些函数和类的集合文件,它能实现一些相应的功能,当我们需要使用这些功能的时候,直接把相应的模块导入到我们的程序中 ...

随机推荐

  1. nefu 197 关键字检索(kmp算法)

    Description 在信息检索系统中一个很重要的环节就是关键字符串的查找,从而很多对自己有用的信息.给你一个很长的一段文字, 和一个关键信息字符串,现在要你判断这段文字里面是否有关键字符串. In ...

  2. TCP/IP,http,socket,长连接,短连接——小结。

    来源:http://blog.chinaunix.net/uid-9622484-id-3392992.html TCP/IP是什么? TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层. ...

  3. url重写步骤

    url重写:在global文件中,在application_BeginRequest事件中1:获取URL string url=Request.AppRelativeCurrentExecutionF ...

  4. iptables查看、添加、删除规则

    1.查看iptables -nvL –line-number -L 查看当前表的所有规则,默认查看的是filter表,如果要查看NAT表,可以加上-t NAT参数-n 不对ip地址进行反查,加上这个参 ...

  5. wpa_supplicant_8_ti hostapd wpa_supplicant TI 官方的wpa_supplicant hostapd 移植到linux

    在移植 wpa_supplicant_8_ti 的时候碰到很多头文件找不到.然后参考了下面的博客 http://blog.csdn.net/penglijiang/article/details/85 ...

  6. How To Add Swap on Ubuntu 14.04

    https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04 How To Add Swap on ...

  7. 如何查看自己电脑支持OpenGL core版本

    1. 起因: 红宝书上的例子在电脑上运行后没有效果,但是怎么也找不到原因,反复对看了书上的源码和代码发现没有任何问题,但是就是没有树上写的效果 2. 思路:查看函数的说明,这里推荐使用docs.gl, ...

  8. Android Matrix类以及ColorMatri

    引自:http://www.chinabaike.com/t/37396/2014/0624/2556217.html Android Matrix类以及ColorMatrix类详解 最近在系统学习了 ...

  9. .bash_profile和.bashrc的什么区别

    /etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/profile.d目录的配置文件中搜集shell的设置./etc/bashrc:为每一个运 ...

  10. Photos FrameWork 续

    1. Model PHAsset .PHAssetCollection.PHCollectionList 是Photos框架中的模型类,PHAsset类模型是图片或者视频文件数据:PHAssetCol ...