Python环境配置

简介:

Python作为Google的第三大开发语言、Dropbox的基础语言已经已经越来越得到人们的喜爱,尤其是运维人员,而且很多优秀的运维工具都是Python开发的,引用Python作者的一句话:人生苦短,我用Python。

下面我就和大家简单分享下我在用Python进行开发时使用的一些Python配置方面的小工具。

Python版本管理

我以前使用pythonbrew来管理我的开发机上面多个Python解析器,现在我使用pyenv来管理我的Python解析器,github地址,安装和使用直接见github里面的README就行了,用起来还是比较简单的,通过pyenv可以管理多个Python版本并共存,随时切换想要使用的版本,且这个工具本身不依赖于Python。

环境依赖

根据12Factor规则,应用程序要显示的声明依赖,这里我能想到的一个很好的例子是RubyGemfile,这个文件里面声明了一个Ruby应用程序所依赖的所有依赖,Gemfile就是对Ruby程序的依赖声明,在Python里面对依赖进行声明我想到了pip

依赖申明好了,不同的应用程序间如何进行依赖隔离呢,同样Ruby程序是用bundle exec,这里为什么要提Ruby,那时因为我以前配置一个Ruby应用程序感觉非常方便,直接执行script/bootstarp所有依赖都安装好了,基本直接运行程序就能跑了。

其实Python里面也有一个非常好的依赖隔离的东西virtualenv,下面简单介绍下基于virtualenv写了一个简单的Python项目类似Rubybundle的小工具。

Virtualenv Exec(ve)

源代码如下:

#!/bin/bash

if [[ $# -le 0 ]]; then
    echo "Usage: `basename $0` <command> [agrument]..."
fi

if [ -r "$HOME/.verc" ]; then
    source "$HOME/.verc"
fi

if [ -z "$VIRTUALENV_DIR" ]; then
    VIRTUALENV_DIR=".virtualenv"
fi

DIR=`pwd`
for (( ;; ))
do
    if [ -r "$DIR/$VIRTUALENV_DIR/bin/activate" ]; then
        source "$DIR/$VIRTUALENV_DIR/bin/activate"
        if [ -n "$VIRTUAL_ENV" ]; then
            break
        fi
    fi
    if [ "$DIR" == "/" ]; then
        break
    fi
    DIR=`dirname $DIR`
done

if [ -z "$VIRTUAL_ENV" ]; then
    echo "virtualenv: not found"
    echo
    exit 1
else
    if [ "$1" == "-v" ]; then
        echo "virtualenv: $VIRTUAL_ENV"
        echo
        shift
    fi
    exec $@
fi

bootstrap.py

源代码如下:

# -*- coding: utf-8 -*-

if __name__ == '__main__':
    exit(1)

import os
import subprocess
import logging
import hashlib
import stat

logger = logging.getLogger(__name__)

class Bootstrap(object):
    working_dir = None
    bootstrap_dir = None
    virtualenv_dir = None


    def __init__(self, working_dir=None):
        self.working_dir = working_dir or os.getcwd()
        self.bootstrap_dir = self._config_dot_dir('BOOTSTRAP_DIR', '.bootstrap')
        self.virtualenv_dir = self._config_dot_dir('VIRTUALENV_DIR', '.virtualenv')

        if not os.path.isdir(self.bootstrap_dir):
            os.makedirs(self.bootstrap_dir)

        logger.debug('working_dir: %s', self.working_dir)
        logger.debug('bootstrap_dir: %s', self.bootstrap_dir)
        logger.debug('virtualenv_dir: %s', self.virtualenv_dir)

        self.install_virtualenv()
        self.install_ve()


    def _config_dot_dir(self, env_name, default_value=None):
        value = os.environ.get(env_name)
        if not value:
            return '%s/%s' % (self.working_dir, default_value)

        logger.debug('config %s from envron', env_name)
        if os.path.isabs(value):
            return value
        else:
            return '%s/%s' % (self.working_dir, value)


    def install_virtualenv(self):
        if os.path.isfile('%s/%s' % (self.virtualenv_dir, 'bin/activate')):
            return

        executable = None
        try:
            executable = subprocess.check_output(['command', '-v', 'virtualenv-12.0.7/virtualenv.py'])
            if not type(executable) is str:
                # convert from bytes to str (unicode) under python3
                executable = executable.decode()
            executable = executable.strip()
        except:
            pass

        if not executable:
            virtualenv_tar = '%s/%s' % (self.bootstrap_dir, 'virtualenv.tar.gz')
            executable = '%s/%s' % (self.bootstrap_dir, 'virtualenv-12.0.7/virtualenv.py')
            self.download('http://mirrors.aliyun.com/pypi/packages/source/v/virtualenv/virtualenv-12.0.7.tar.gz', 
                         virtualenv_tar, 'e08796f79d112f3bfa6653cc10840114')
            os.system('tar -zxvf "%s" -C "%s"' % (virtualenv_tar, self.bootstrap_dir))

        os.chdir(self.bootstrap_dir)
        try:
            os.system('%s --distribute --no-site-packages "%s"' % (executable, self.virtualenv_dir))
        finally:
            os.chdir(self.working_dir)


    def download(self, source, target, hashing=None):
        #if hashing and os.path.isfile(target) and hashing == self.md5sum(target):
            #return
        if os.system('wget "%s" -O "%s"' % (source, target)) == 0:
            return
        if os.system('curl "%s" -o "%s"' % (source, target)) == 0:
            return

        logger.error('Unable to download "%s"' % source)
        raise RuntimeError


    def mark_executable(self, path):
        st = os.stat(path)
        os.chmod(path, st.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH)


    def md5sum(self, filename):
        return hashlib.md5(open(filename, 'rb').read()).hexdigest()


    def install_ve(self):
        executable = '%s/%s' % (self.bootstrap_dir, 've')
        self.download('https://raw.github.com/baijian/ve/v1.1/ve',
                executable, 'fd0f0601c6732ca9a5b3b62e691d68cb')
        self.mark_executable(executable)


    def ve(self, cmd):
        os.system('%s/%s %s' % (self.bootstrap_dir, 've', cmd))


_bootstrap = None

__all_ = ['bootstrap', 've']

def bootstrap(working_dir=None):
    global _bootstrap
    _bootstrap = Bootstrap(working_dir)

def ve(cmd):
    global _bootstrap
    if not _bootstrap:
        bootstrap()
    _bootstrap.ve(cmd)

开始使用

  • 创建项目目录
mkdir project && mkdir project/script && cd project
  • 编写脚手架
#!/usr/bin/env python
# -*- coding: utf8 -*-

import os
source = "https://raw.github.com/baijian/bootstrap.py/v1.2/bootstrap.py"
target = "%s/bootstrap.py" % os.path.dirname(os.path.abspath(__file__))
if not os.path.isfile(target):
    os.system("wget '%s' -O %s" % (source, target))


from bootstrap import bootstrap, ve
bootstrap(os.path.dirname(os.path.abspath(__file__)) + "/..")

ve("pip install requests==2.4.1")
  • 修改依赖申明(可选)

编辑并修改script/bootstrap文件

  • 安装项目依赖
script/bootstrap
  • 查看项目依赖
ve pip freeze
  • 查看项目使用的Python解析器
ve command -v python

其他

如果要使用Python进行web应用的开发,可以选用的框架诸如FlaskDjangoTornadoweb.py,提到web.py,想到了互联网之子亚伦·斯沃茨,有兴趣可以看看他的视频~

还有一些有些的优秀的Python运维工具诸如:AnsibleSaltstackFabricSupervisord等,Python的开源模块的质量非常高,这也是我喜欢Python的一个很重要的原因。

目录
相关文章
|
1月前
|
Unix Shell Linux
赞!优雅的Python多环境管理神器!易上手易操作!
赞!优雅的Python多环境管理神器!易上手易操作!
|
2月前
|
IDE Linux 开发工具
技术小白如何配置Python环境
技术小白如何配置Python环境
|
3月前
|
机器学习/深度学习 并行计算 算法框架/工具
在Python中进行深度学习环境准备
在Python中进行深度学习环境准备
51 4
|
12天前
|
数据采集 JavaScript C++
Python搭建编程环境-安装Python3解释器
Python搭建编程环境-安装Python3解释器
24 1
|
14天前
|
jenkins 测试技术 持续交付
软件测试|docker搭建Jenkins+Python+allure自动化测试环境
通过以上步骤,你可以在Docker中搭建起Jenkins自动化测试环境,实现Python测试的自动化执行和Allure报告生成。 买CN2云服务器,免备案服务器,高防服务器,就选蓝易云。百度搜索:蓝易云
34 6
|
1月前
|
Shell Python Windows
通过Python实现win11环境下FTP的上传与下载
通过Python实现win11环境下FTP的上传与下载
|
2月前
|
存储 安全 Python
如何在Python中实现一个单例模式,确保在多线程环境中也是安全的?
【2月更文挑战第5天】【2月更文挑战第11篇】如何在Python中实现一个单例模式,确保在多线程环境中也是安全的?
|
2月前
|
数据安全/隐私保护 iOS开发 MacOS
|
2月前
|
存储 编译器 Python
python实战【外星人入侵】游戏并改编为【梅西vsC罗】(球迷整活)——搭建环境、源码、读取最高分及生成可执行的.exe文件
python实战【外星人入侵】游戏并改编为【梅西vsC罗】(球迷整活)——搭建环境、源码、读取最高分及生成可执行的.exe文件
|
3月前
|
Linux Docker Python
Python实战技术 - Python虚拟隔离环境 和 Docker技术
Python实战技术 - Python虚拟隔离环境 和 Docker技术
199 0