赞
踩
大家好,小编来为大家解答以下问题,python自动安装第三方库的命令,python自动安装软件并执行,今天让我们一起来看看吧!
本篇文章给大家谈谈python自动安装第三方库的命令,以及python自动安装软件并执行,希望对各位有所帮助,不要忘了收藏本站喔。
以flask为例,使用指令
pip install flask
即可安装
其他选项:
- install 安装库
- uninstall 卸载库
- list 列出已经安装的库
- show 列出已安装的库的详细信息
- search 通过PyPI搜索库
- help 帮助命令
官网获取源文件,进行安装
官网获取whl文件进行安装,这样的好处是可以离线安装,但是如果一个一个的获取whl文件,会比较麻烦,而且,不同的包会存在依赖,所以我们最好的方式是通过pip 在线安装,然后获取到whl文件的路径,后按照路径批量下载,后按照顺序进行安装怎样用python画弧形花瓣。
pip install pipenv
pipenv环境创建以及使用特定python版本
- pipenv --python=/usr/bin/python3 #指定python版本
- pipenv shell # 创建虚拟环境
- exit #退出环境
- pipenv --rm #删除环境
- pip list
- pip install flask
从中可以看到下载各种库的记录很规整,可以进行使用,要使用的话可以将下载的返回内容导入一个requirement.txt文件内python必背简单代码。
指令:pip install flask >> requirement.txt
requirement.txt内容
Looking in indexes: http://pypi.douban.com/simple Collecting flask Downloading http://pypi.doubanio.com/packages/cd/77/59df23681f4fd19b7cbbb5e92484d46ad587554f5d490f33ef907e456132/Flask-2.0.3-py3-none-any.whl (95 kB) Collecting itsdangerous>=2.0 Downloading http://pypi.doubanio.com/packages/9c/96/26f935afba9cd6140216da5add223a0c465b99d0f112b68a4ca426441019/itsdangerous-2.0.1-py3-none-any.whl (18 kB) Collecting click>=7.1.2 Downloading http://pypi.doubanio.com/packages/4a/a8/0b2ced25639fb20cc1c9784de90a8c25f9504a7f18cd8b5397bd61696d7d/click-8.0.4-py3-none-any.whl (97 kB) Collecting Werkzeug>=2.0 Downloading http://pypi.doubanio.com/packages/f4/f3/22afbdb20cc4654b10c98043414a14057cd27fdba9d4ae61cea596000ba2/Werkzeug-2.0.3-py3-none-any.whl (289 kB) Collecting Jinja2>=3.0 Downloading http://pypi.doubanio.com/packages/20/9a/e5d9ec41927401e41aea8af6d16e78b5e612bca4699d417f646a9610a076/Jinja2-3.0.3-py3-none-any.whl (133 kB) Collecting importlib-metadata Downloading http://pypi.doubanio.com/packages/a0/a1/b153a0a4caf7a7e3f15c2cd56c7702e2cf3d89b1b359d1f1c5e59d68f4ce/importlib_metadata-4.8.3-py3-none-any.whl (17 kB) Collecting MarkupSafe>=2.0 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) Collecting dataclasses Downloading http://pypi.doubanio.com/packages/fe/ca/75fac5856ab5cfa51bbbcefa250182e50441074fdc3f803f6e76451fab43/dataclasses-0.8-py3-none-any.whl (19 kB) Collecting zipp>=0.5 Downloading http://pypi.doubanio.com/packages/bd/df/d4a4974a3e3957fd1c1fa3082366d7fff6e428ddb55f074bf64876f8e8ad/zipp-3.6.0-py3-none-any.whl (5.3 kB) Collecting typing-extensions>=3.6.4 Downloading http://pypi.doubanio.com/packages/45/6b/44f7f8f1e110027cf88956b59f2fad776cca7e1704396d043f89effd3a0e/typing_extensions-4.1.1-py3-none-any.whl (26 kB) Installing collected packages: zipp, typing-extensions, MarkupSafe, importlib-metadata, dataclasses, Werkzeug, Jinja2, itsdangerous, click, flask 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
参考文章:https://www.cnblogs.com/nwnusun/p/16970717.html
代码如下:
- import sys
- import os
- import re
- import subprocess
-
- baseDir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
- sys.path.append(baseDir)
- file_path = 'requirement.txt'
- mm = []
-
- def command_func(command):
- #command = command
- #command = 'wget {}'.format(n[2])
- process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- process.wait()
- # 获取命令的输出和错误信息
- output = process.stdout.read()
- error = process.stderr.read()
- # 将输出和错误信息解码为字符串
- output = output.decode(encoding="utf-8")
- error = error.decode(encoding="utf-8")
- # 返回命令的输出和错误信息
- result = {"output": output, "error": error}
- #print(result)
-
- with open(file_path,mode='rt') as file_object:
- for line in file_object:
- if line.startswith(' Dow'):
- #if 'Downloading' in line:
- #mm.append(line)
- n = re.split(r"\s+",line)
- mm.append(n[2])
- #command = 'wget {}'.format(n[2])
- #command_func(command)
-
- file_pip_path = 'install.txt'
- with open(file_pip_path,'wt') as file_object2:
- for url in mm:
- #print(url)
- file = url.split('/')[-1]
- file_object2.write(file+'\n')
- #print(url)
- # for url in mm:
- # file = url.split('/')[-1]
- # print(file)
-
- mm = os.listdir()
- base_dir = os.path.dirname(os.path.abspath(__file__))
- name = 'package'
- file_path = os.path.join(base_dir,name)
- for file in mm:
- if file.endswith('whl'):
- command = 'mv {} {}'.format(file,file_path)
- command_func(command)
获取到的install.txt文件内容:
- Flask-2.0.3-py3-none-any.whl
- itsdangerous-2.0.1-py3-none-any.whl
- click-8.0.4-py3-none-any.whl
- Werkzeug-2.0.3-py3-none-any.whl
- Jinja2-3.0.3-py3-none-any.whl
- importlib_metadata-4.8.3-py3-none-any.whl
- MarkupSafe-2.0.1-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl
- dataclasses-0.8-py3-none-any.whl
- zipp-3.6.0-py3-none-any.whl
- typing_extensions-4.1.1-py3-none-any.whl
- import os
- import subprocess
- base_dir = os.path.dirname(os.path.abspath(__file__))
- file = 'install.txt'
- file_path = os.path.join(base_dir,file)
-
- def command_func(command):
- #command = command
- #command = 'wget {}'.format(n[2])
- process = subprocess.Popen(command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
- process.wait()
- # 获取命令的输出和错误信息
- output = process.stdout.read()
- error = process.stderr.read()
- # 将输出和错误信息解码为字符串
- output = output.decode(encoding="utf-8")
- error = error.decode(encoding="utf-8")
- # 返回命令的输出和错误信息
- result = {"output": output, "error": error}
- print(result)
-
- with open(file_path,mode='rt') as file_object:
- for file in file_object:
- command = "pip install {}".format(file)
- command_func(command)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。