Windows 上的 sys.prefix 是 python 文件夹,但 linux 它通常指向 /usr,这意味着必须按名称搜索目录,除非有其他方法。
在那儿?
依赖文件夹名称也很混乱,'Python27','python2.7'只是命名频繁的。
这个脚本是不是有点矫枉过正?我这样做对吗?
os.environ 无能为力,因为可能没有 PYTHON_PATH,或者运行的内容可能不匹配
sys.executable 无济于事,因为它只适用于 Windows。
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# rev 0.0.0.1
import os, platform, sys
"""File: _python_pfn.py
Seems excessive...
3 imports, 3 functions, 30 lines just to
determine current python installation parent path, python folder name, and full path
"""
def _python_parent():
"""assume linux, but if windows just return sys.prefix"""
ret = os.path.join('{}'.format(sys.prefix), 'lib')
if platform.uname()[0] == 'Windows':
ret = '{}'.format(sys.prefix)
return ret
def _python_name(path):#, major=None, minor=None
"""assume windows, but if not listdir and find"""
ma, mi = (str(sys.version_info[0]), str(sys.version_info[1]))
#if major is not None: ma = str(major)
#if minor is not None: ma = str(minor)
ret = os.path.split(path)[1]
if platform.uname()[0] != 'Windows':
ls = [k for k in os.listdir(path) if os.path.isdir(os.path.join(path, k)) ]
for k in ls:
if k.lower().find('python') != -1:
if k.find(ma) != -1:
if k.find(mi) != -1:
ret = k
return ret
def _python_path(path):
"""use as _python_path(_python_parent())"""
return os.path.join(path, _python_name(path))