2016-11-10 24 views
3

我的Python应用程序可以与pip是安装正常的方式,或在开发/编辑模式,就像这样:检查我的应用程序开发/编辑模式下运行

virtualenv my_app 
source my_app/bin/activate 
pip install -e my_app 

我怎样才能让一个函数来反思我的virtualenv并检查我的应用程序是否在开发/可编辑模式下运行?

sys模块中有没有“标志”?

动机:在开发模式和生产中有不同的配置。

编辑:我比较的virtualenv目录。

import os 
import sys 

import pkg_resources 

main_pkg = 'my_app' 
package_dir = pkg_resources.resource_filename(main_pkg, '__init__.py') 
virtualenv_dir = os.path.dirname(os.path.dirname(sys.executable)) 
common_path = os.path.commonprefix([package_dir, virtualenv_dir]) 
is_dev_mode = not common_path.startswith(virtualenv_dir) 

我测试,如果package_dir是的virtualenv_dir子目录:如果它不是一个子目录,然后我对发展模式。

EDIT2:

有没有更可靠的解决方案?

我想知道在环境中是否没有数据/标志会明确指示我的应用程序正在运行开发模式。

如果另一个依赖关系在开发模式下会发生什么?

回答

3

pip使用代码,我们可以判断这一点:

import pip 
import pkg_resources 

# I've done `pip install -e .` for a git repo I'm working in inside 
# a virtualenv 
distributions = {v.key: v for v in pkg_resources.working_set} 
# >>> distribution 
# pre-commit 0.9.3 (/home/asottile/workspace/pre-commit) 
distribution = distributions['pre-commit'] 

# Below is approximately how `pip freeze` works, see the end of 
# the answer for a simpler approach, still using pip 

# Turn into a pip FrozenRequirement (I'm using pip 9.0.1, it may 
# be different for your version) 
# I've passed an empty list for the second argument (dependency_links) 
# I don't think it's necessary? 
frozen_requirement = pip.FrozenRequirement.from_dist(distribution, []) 

# Query whether the requirement is installed editably: 
print(frozen_requirement.editable) 

这样做的魔力来自内部PIP一个小功能(pip.utils):

def dist_is_editable(dist): 
    """Is distribution an editable install?""" 
    for path_item in sys.path: 
     egg_link = os.path.join(path_item, dist.project_name + '.egg-link') 
     if os.path.isfile(egg_link): 
      return True 
    return False 

在此的dist是一个pkg_resources分布(正如我们上面获得的)。当然,你可以直接使用dist_is_editable功能,而不是通过FrozenRequirement打算:

# With `distribution` as above: 
from pip.utils import dist_is_editable 
print(dist_is_editable(distribution)) # True in my case ;) 
+0

听起来不错;-)'pkg_resources.working_set'软件包的名称('import pkg_name')或库名称(如setup.py中的'name =“lib_name”)的密钥? –

+0

这些键最终会成为“规范化”的软件包名称(所以对于'yaml'你会寻找'pyyaml',对于'pre_commit'你会寻找'pre-commit')。规范化规则遵循https://www.python.org/dev/peps/pep-0440/(PEP440)(尽管通常它最终是'.replace('_',' - ')。lower()') –

+0

@LaurentLAPORTE你对这个问题还有其他问题吗? –

0

我不确定是否有可靠的方法来确定这一点。事实上,我会建议你不要这样测试路径,因为你可能遇到不同的操作系统或环境中的特殊情况。

有几个备选方案,我建议改为:

1)如果这是一个命令行工具,我建议允许通过一个命令行标志,自定义配置文件配置的加载:

from argparse import ArgumentParser 
import sys 
import json 

parser = ArgumentParser(description='...') 
parser.add_argument('-c', '--config', default='config.json') 

def main(argv): 
    args = parser.parse_args(argv) 
    print('loading config file:', args.config) 
    with open(args.config, 'r') as config: 
     config = json.loads(config.read()) 
    print('loaded config', config) 
    # TODO do something with the config 

if __name__ == '__main__': 
    main(sys.argv[1:]) 

运行带:python3 test1.py -c config-dev.json

2)如果这不是一个CLI应用程序,你可以实现通过USI类似的事情NG环境变量:

import os 
import json 

os.environ.get('CONFIG', 'config.json') 

def main(): 
    config_file = os.environ.get('CONFIG', 'config.json') 
    print('loading config file:', config_file) 
    with open(config_file, 'r') as config: 
     config = json.loads(config.read()) 
    print('loaded config', config) 
    # TODO do something with the config 

if __name__ == '__main__': 
    main() 

运行:CONFIG=config-dev.json python3 test2.py,或:

export CONFIG=config-dev.json 
python3 test2.py 

你也可以做一个shell脚本,以帮助设置你的开发环境,我们称之为customenv

source env/bin/activate 
export CONFIG=config-dev.json 

然后你可以使用这个文件来激活开发环境:

source customenv 

3)如果你真的想在你的开发环境代码的特殊情况下,可以通过环境变量也指定此:

import os 
is_dev_mode = 'MY_APP_DEV' in os.environ and os.environ['MY_APP_DEV'] == '1' 
if is_dev_mode: 
    print('dev mode!') 

运行与MY_APP_DEV=1 python3 test3.py,或:

export MY_APP_DEV=1 
python3 -m test3.py 

4)更多定制:

import os 
app_mode = os.environ.get('MY_APP_MODE', 'prod') 
print(app_mode) 
+0

我同意你的观点,但是这不是我想要的。对不起,但是,我真的需要检查我是否处于*开发*模式。 –

相关问题