Linux搭建python环境编写脚本并使用

100 阅读1分钟
概览
阅读人群后端,运维
本文字数约等于 829 字
阅读时间约等于 5 分钟
涉及知识python,Linux,shell

安装 python 和 pip 工具


# python3
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

# python2
curl https://bootstrap.pypa.io/pip/2.7/get-pip.py -o get-pip.py

# 执行脚本
python get-pip.py

修改 pip 的配置

cp ~/.pip/pip.conf ~/.pip/pip.conf.bak$(date +%y%m%d%H%M%S)

cat>~/.pip/pip.conf<<EOF
[global]
trusted-host =  mirrors.aliyun.com
index-url = http://mirrors.aliyun.com/pypi/simple

EOF

#安装脚本依赖包
#未设置https的情况下需要这样安装
pip install pyyaml -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com

python 脚本编写

创建 python 的虚拟环境

个人理解虚拟环境的作用是为了隔离 python 各种依赖包版本可能不兼容而产生的,这样每个应用都有自己的依赖包管理环境,其他应用的依赖包的升级并不会相互影响,使得应用更加稳定

mkdir ./venv
# 创建 python 虚拟环境
python -m venv ./venv
# 激活虚拟环境
source ./venv/bin/activate
# 退出虚拟环境
deactivate
# 删除虚拟环境
rm -rf ./venv

编写脚本

import click
@click.command()
@click.option('--count',default=1,type=int,help='your count')
@click.argument('name',type=str,default="zarek")
def hello(count,name):
  click.echo("hello world")
  click.echo("your name is "+name)
  click.echo(count)
  click.echo(click.style('this is a error',fg='red'))
  click.echo(click.style('this is a text',blink=True,bold=True,fg='red'))
  click.echo(click.style('this is a text2',bold=True,fg='red'))
  #click.echo(c)

if __name__ == '__main__':
  hello()

python 打包的两种方式

通过 pyinstaller 将依赖包和脚本打成一个单个可执行的二进制文件

# linux 系统安装前置依赖包
yum install -y gcc zlib zlib-devel

# 安装 pyinstall
pip install pyinstaller

# 对python脚本(demo.py)进行打包
pyinstaller -F -w demo.py

发布应用到 pypi 仓库中

# 安装所需要的依赖包
pip3 install setup wheel -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com

# 目录结构
├── build
├── dist
├── README.md
├── setup.py
├── src
│   ├── cli.py
│   ├── pypicli.egg-info
│   └── zarekpypicli.egg-info
└── venv

# 编写打包描述文件 setup.py
import setuptools

setuptools.setup(
  name='pypicli',
  version="0.0.1",
  author="zarek",
  author_email="1999",
  description="pypi cli example",
  long_description="#pypicli",
  long_description_content_type="text/markdown",
  packages=setuptools.find_packages(),
  py_modules=["cli"],
  package_dir={"":"src"},
  install_requires=[
      "click"
      ],
  entry_points={
          'console_scripts':[
          'pypix=src/cli:hello'
          ]
      }
)

# 对脚本进行打包
python3 setup.py sdist bdist_wheel

# 检查打出来包是否错误
twine check dist/*

# 上传包到 pypi 仓库
twine upload --repository testpypi dist/*

# 下载上传的包
pip install -i https://.../ pypicli==0.0.1

踩坑点

执行 pyinstaller 的时候可能会提示命令找不到,此时需要将该命令加到环境变量中去

# 查看当前用户的环境变量是否包含 pyinstaller
echo $PATH

# 在配置文件中加入pyinstaller的路径
echo "PATH=$PATH:$(pip3 show twine | grep Location | awk '{print $2}')"  >> /home/zzz/.bash_profile

# 使修改命令生效
source /home/zzz/.bash_profile

附录

shell 执行 python 脚本

# 第一种方式
json='{"name":"zhaoruihang","age":"12"}'
cat>test.py<<EOF
import json
data='${json}'
dict_json = json.loads(data)
for key in  dict_json :
  print (key+'='+dict_json[key])

EOF
python test.py > 1.cfg

# 第二种方式
json='{"name":"zhaoruihang","age":"12"}'
python -c "
import json
data='${json}'
dict_json = json.loads(data)
for key in  dict_json :
  print (key+'='+dict_json[key])

" > 1.cfg

参考文章

www.php.cn/python-tuto… blog.csdn.net/libaineu200… www.cnblogs.com/yinhaiping/… blog.csdn.net/clean_water… cloud.tencent.com/developer/a… cloud.tencent.com/developer/a… blog.csdn.net/weixin_4026…