当前位置:   article > 正文

python自动安装windows软件,python自动化运行软件_python自动化安装软件

python自动化安装软件

大家好,小编来为大家解答以下问题,python自动安装第三方库的命令,python自动安装软件并执行,今天让我们一起来看看吧!

本篇文章给大家谈谈python自动安装第三方库的命令,以及python自动安装软件并执行,希望对各位有所帮助,不要忘了收藏本站喔。

1.第三方库安装方式

1.1 pip 安装

flask为例,使用指令

pip install flask

即可安装
其他选项:

  1. install 安装库
  2. uninstall 卸载库
  3. list 列出已经安装的库
  4. show 列出已安装的库的详细信息
  5. search 通过PyPI搜索库
  6. help 帮助命令
1.2 源码安装

官网获取源文件,进行安装

1.3 pip 离线安装whl

官网获取whl文件进行安装,这样的好处是可以离线安装,但是如果一个一个的获取whl文件,会比较麻烦,而且,不同的包会存在依赖,所以我们最好的方式是通过pip 在线安装,然后获取到whl文件的路径,后按照路径批量下载,后按照顺序进行安装怎样用python画弧形花瓣

2. pipenv环境安装与使用

2.1 pipenv安装和环境创建
pip install pipenv

pipenv环境创建以及使用特定python版本

  1. pipenv --python=/usr/bin/python3 #指定python版本
  2. pipenv shell # 创建虚拟环境
  3. exit #退出环境
  4. pipenv --rm #删除环境

在这里插入图片描述
在这里插入图片描述

3.flask安装与whl文件路径url导出

3.1 flask 的pip安装
  1. pip list
  2. pip install flask

在这里插入图片描述

3.2 获取whl文件路径并导出url

从中可以看到下载各种库的记录很规整,可以进行使用,要使用的话可以将下载的返回内容导入一个requirement.txt文件内python必背简单代码
指令:pip install flask >> requirement.txt
requirement.txt内容

  1. Looking in indexes: http://pypi.douban.com/simple
  2. Collecting flask
  3. Downloading http://pypi.doubanio.com/packages/cd/77/59df23681f4fd19b7cbbb5e92484d46ad587554f5d490f33ef907e456132/Flask-2.0.3-py3-none-any.whl (95 kB)
  4. Collecting itsdangerous>=2.0
  5. Downloading http://pypi.doubanio.com/packages/9c/96/26f935afba9cd6140216da5add223a0c465b99d0f112b68a4ca426441019/itsdangerous-2.0.1-py3-none-any.whl (18 kB)
  6. Collecting click>=7.1.2
  7. Downloading http://pypi.doubanio.com/packages/4a/a8/0b2ced25639fb20cc1c9784de90a8c25f9504a7f18cd8b5397bd61696d7d/click-8.0.4-py3-none-any.whl (97 kB)
  8. Collecting Werkzeug>=2.0
  9. Downloading http://pypi.doubanio.com/packages/f4/f3/22afbdb20cc4654b10c98043414a14057cd27fdba9d4ae61cea596000ba2/Werkzeug-2.0.3-py3-none-any.whl (289 kB)
  10. Collecting Jinja2>=3.0
  11. Downloading http://pypi.doubanio.com/packages/20/9a/e5d9ec41927401e41aea8af6d16e78b5e612bca4699d417f646a9610a076/Jinja2-3.0.3-py3-none-any.whl (133 kB)
  12. Collecting importlib-metadata
  13. Downloading http://pypi.doubanio.com/packages/a0/a1/b153a0a4caf7a7e3f15c2cd56c7702e2cf3d89b1b359d1f1c5e59d68f4ce/importlib_metadata-4.8.3-py3-none-any.whl (17 kB)
  14. Collecting MarkupSafe>=2.0
  15. Downloading http://pypi.doubanio.com/packages/e2/a9/eafee9babd4b3aed918d286fbe1c20d1a22d347b30d2bddb3c49919548fa/MarkupSafe-2.0.1-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (30 kB)
  16. Collecting dataclasses
  17. Downloading http://pypi.doubanio.com/packages/fe/ca/75fac5856ab5cfa51bbbcefa250182e50441074fdc3f803f6e76451fab43/dataclasses-0.8-py3-none-any.whl (19 kB)
  18. Collecting zipp>=0.5
  19. Downloading http://pypi.doubanio.com/packages/bd/df/d4a4974a3e3957fd1c1fa3082366d7fff6e428ddb55f074bf64876f8e8ad/zipp-3.6.0-py3-none-any.whl (5.3 kB)
  20. Collecting typing-extensions>=3.6.4
  21. Downloading http://pypi.doubanio.com/packages/45/6b/44f7f8f1e110027cf88956b59f2fad776cca7e1704396d043f89effd3a0e/typing_extensions-4.1.1-py3-none-any.whl (26 kB)
  22. Installing collected packages: zipp, typing-extensions, MarkupSafe, importlib-metadata, dataclasses, Werkzeug, Jinja2, itsdangerous, click, flask
  23. Successfully installed Jinja2-3.0.3 MarkupSafe-2.0.1 Werkzeug-2.0.3 click-8.0.4 dataclasses-0.8 flask-2.0.3 importlib-metadata-4.8.3 itsdangerous-2.0.1 typing-extensions-4.1.1 zipp-3.6.0

4.自动化下载whl与安装

4.1 python环境调用shell指令

参考文章:https://www.cnblogs.com/nwnusun/p/16970717.html

4.2 自动化下载whl程序

代码如下:

  1. import sys
  2. import os
  3. import re
  4. import subprocess
  5. baseDir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
  6. sys.path.append(baseDir)
  7. file_path = 'requirement.txt'
  8. mm = []
  9. def command_func(command):
  10. #command = command
  11. #command = 'wget {}'.format(n[2])
  12. process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  13. process.wait()
  14. # 获取命令的输出和错误信息
  15. output = process.stdout.read()
  16. error = process.stderr.read()
  17. # 将输出和错误信息解码为字符串
  18. output = output.decode(encoding="utf-8")
  19. error = error.decode(encoding="utf-8")
  20. # 返回命令的输出和错误信息
  21. result = {"output": output, "error": error}
  22. #print(result)
  23. with open(file_path,mode='rt') as file_object:
  24. for line in file_object:
  25. if line.startswith(' Dow'):
  26. #if 'Downloading' in line:
  27. #mm.append(line)
  28. n = re.split(r"\s+",line)
  29. mm.append(n[2])
  30. #command = 'wget {}'.format(n[2])
  31. #command_func(command)
  32. file_pip_path = 'install.txt'
  33. with open(file_pip_path,'wt') as file_object2:
  34. for url in mm:
  35. #print(url)
  36. file = url.split('/')[-1]
  37. file_object2.write(file+'\n')
  38. #print(url)
  39. # for url in mm:
  40. # file = url.split('/')[-1]
  41. # print(file)
  42. mm = os.listdir()
  43. base_dir = os.path.dirname(os.path.abspath(__file__))
  44. name = 'package'
  45. file_path = os.path.join(base_dir,name)
  46. for file in mm:
  47. if file.endswith('whl'):
  48. command = 'mv {} {}'.format(file,file_path)
  49. command_func(command)

获取到的install.txt文件内容:

  1. Flask-2.0.3-py3-none-any.whl
  2. itsdangerous-2.0.1-py3-none-any.whl
  3. click-8.0.4-py3-none-any.whl
  4. Werkzeug-2.0.3-py3-none-any.whl
  5. Jinja2-3.0.3-py3-none-any.whl
  6. importlib_metadata-4.8.3-py3-none-any.whl
  7. MarkupSafe-2.0.1-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
  8. dataclasses-0.8-py3-none-any.whl
  9. zipp-3.6.0-py3-none-any.whl
  10. typing_extensions-4.1.1-py3-none-any.whl
4.3 pip安装whl模块
  1. import os
  2. import subprocess
  3. base_dir = os.path.dirname(os.path.abspath(__file__))
  4. file = 'install.txt'
  5. file_path = os.path.join(base_dir,file)
  6. def command_func(command):
  7. #command = command
  8. #command = 'wget {}'.format(n[2])
  9. process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
  10. process.wait()
  11. # 获取命令的输出和错误信息
  12. output = process.stdout.read()
  13. error = process.stderr.read()
  14. # 将输出和错误信息解码为字符串
  15. output = output.decode(encoding="utf-8")
  16. error = error.decode(encoding="utf-8")
  17. # 返回命令的输出和错误信息
  18. result = {"output": output, "error": error}
  19. print(result)
  20. with open(file_path,mode='rt') as file_object:
  21. for file in file_object:
  22. command = "pip install {}".format(file)
  23. command_func(command)
文章知识点与官方知识档案匹配,可进一步学习相关知识
Python入门技能树首页概览424943 人正在系统学习中
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/笔触狂放9/article/detail/706505
推荐阅读
相关标签
  

闽ICP备14008679号