谈谈我对python sys.path的理解

版权声明 本站原创文章 由 萌叔 发表 转载请注明 萌叔 | http://vearne.cc 前段时间在配置apache服务器时用到了这个参数,所以就特别查了一下 >>> import sys >>> print sys.path ['', '/home/pig', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6', '/usr/lib/python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0', '/usr/local/lib/python2.6/dist-packages'] >>> 在python 调试模式中导入sys模块,就可以打印出它的值。 python 的官方文档关于此值是这样说的。 A list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default. 也就是说python解释器使用此值来,搜索模块,同时这个列表是由两部分组成的,一部分是从环境变量PYTHONPATH中取出的,另一部分中是安装时的默认值。 我做了测试,默认情况下,是没有这个环境变量的,因此我在环境变量中加入了PYTHONPATH 在~/.bashrc 中加入以下内容 PYTHONPATH="/home/pig" export PYTHONPATH 大家可以看到在上面的运行结果中,已经生效了。除了这个,大家还能看到 1)此列表的第一个元素是一个空字符串,它表示python解释器运行的当前目录 2) /usr/lib/python2.6 像这种都是python相应库的安装目录 最后再来看一个实验,打开两个窗口,各启动一个python 解释器,修改其中其中一个sys.path,看对另一个解释器是否有影响 1) >>> import sys >>> print len(sys.path) 14 >>> sys.path.append('/home/aotian/test') >>> print len(sys.path) 15 >>> >>> import sys >>> print len(sys.path) 14 >>> print len(sys.path) 14 >>> 从对比结果可以看出,刚开始两个解释器的len(sys.path)都等于14,给第一个解释器增加元素,第二个解释器并没有收到影响。 ...

January 2, 2018 · 1 min

python 模块==命名空间?

版权声明 本站原创文章 由 萌叔 发表 转载请注明 萌叔 | http://vearne.cc 起因: 想利用模块传递某个变量,修改某个变量的值,且在其它模块中也可见 于是我做了这样一个实验: git@github.com:vearne/test_scope.git base.py value = 10 b.py import base def hello(): print 'scope base', base.value, id(base.value) main.py from base import value from b import hello print 'scope base', value, id(value) value = 20 print 'scope local', value, id(value) hello() 运行python main.py 输出结果如下: ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'hello', 'value'] scope base 10 140195531889072 ['__builtins__', '__doc__', '__file__', '__name__', '__package__', 'hello', 'value'] scope local 20 140195531888832 scope base 10 140195531889072 大家可以看出,value 的值并没有被修改,并且id值(对象的内存地址) 不一致,因此我们得出结论, value 和 base.value 存在在不同位置,是两个不同的对象。 阅读python官方文档 https://docs.python.org/2/tutorial/modules.html 我找到这样一段话 ...

January 1, 2018 · 2 min

在一个Python脚本中加载2种不同版本的库

起因: 从ES集群A往ES集群B导数,然后比对2个ES的数据差异,逐个ID比对。由于ES集群A的版本是1.4.x,ES集群B的版本是5.3.x,所以无法使用同一个ES client包 1. 加载不同版本的client包 对比的过程是,取相同发布时间区间的文章ID,然后比对id的差异 伪码如下: es_A_ids = get_es_A_ids() es_B_ids = get_es_B_ids() diff_ids = es_A_ids - es_B_ids 可以想到的是在访问完集群A后重新加载elasticsearch 库 ## load elasticsearch==1.4.0 es_A_ids = get_es_A_ids() ## load elasticsearch==5.3.0 es_B_ids = get_es_B_ids() diff_ids = es_A_ids - es_B_ids 但是很有趣的是,elasticsearch在load完上一个版本以后,它的版本没有发生变化 2. 清理已经load 的module 经过查资料,我明确了这个问题,python的module,只会被load,有且一次,所以要保证不同版本的module被再次load,只能先clear 原先load的module ES的module都以elasticsearch开头,因此把它们都清理掉 for key in sys.modules.keys(): if key.startswith('elasticsearch'): del sys.modules[key] 完整代码 import importlib import sys # use elasticsearch 5.x es_lib_path = "/Users/woshiaotian/es_5x/lib/python2.7/site-packages" # 注意要把es_lib_path放在sys.path 的首位,确保load module的时候,该目录下的ES库,能够被优先加载 sys.path.insert(0, es_lib_path) #print sys.path elasticsearch = importlib.import_module("elasticsearch") print elasticsearch sys.path.pop(0) for key in sys.modules.keys(): if key.startswith('elasticsearch'): print key del sys.modules[key] # use elasticsearch 1.x es_lib_path = "/Users/woshiaotian/es_1x/lib/python2.7/site-packages" sys.path.insert(0, es_lib_path) elasticsearch = importlib.import_module("elasticsearch") print elasticsearch sys.path.pop(0) #print sys.path for key in sys.modules.keys(): if key.startswith('elasticsearch'): print key del sys.modules[key] sys.modules This is a dictionary that maps module names to modules which have already been loaded. This can be manipulated to force reloading of modules and other tricks. However, replacing the dictionary will not necessarily work as expected and deleting essential items from the dictionary may cause Python to fail. ...

January 1, 2018 · 1 min