如何导出python安装的所有模块名称和版本号到文件中
作者:Fan72 时间:2022-07-26 15:38:59
Python 模块
概念
python中的模块是什么?简而言之,在python中,一个文件(以“.py”为后缀名的文件)就叫做一个模块,每一个模块在python里都被看做是一个独立的文件。模块可以被项目中的其他模块、一些脚本甚至是交互式的解析器所使用,它可以被其他程序引用,从而使用该模块里的函数等功能,使用Python中的标准库也是采用这种方法。
分类
在Python中模块分为以下几种:
系统内置模块,例如:sys、time、json模块等等;
自定义模块,自定义模块是自己写的模块,对某段逻辑或某些函数进行封装后供其他函数调用。注意:自定义模块的命名一定不能和系统内置的模块重名了,否则将不能再导入系统的内置模块了。例如:自定义了一个sys.py模块后,再想使用系统的sys模块是不能使用的;
第三方的开源模块:这部分模块可以通过pip install进行安装,有开源的代码;
一般查看python下安装了什么包,我们可在命令行模式下输入
$ pip list
输出结果:
Package Version
---------------------------------- ---------
absl-py 0.9.0
alabaster 0.7.12
altgraph 0.16.1
anaconda-client 1.7.2
anaconda-navigator 1.9.7
anaconda-project 0.8.3
asn1crypto 1.0.1
…
2
也可以
$ pip freeze
输出结果:
absl-py==0.9.0
alabaster==0.7.12
altgraph==0.16.1
anaconda-client==1.7.2
anaconda-navigator==1.9.7
anaconda-project==0.8.3
asn1crypto==1.0.1
…
在python代码中,可以用os.popen方法调用命令行代码,返回输出的对象,然后再将此写入文件中:
import os
installed_module_list = os.popen("pip freeze")
# print(installed_module_list)
with open("requirements.txt",'w') as f:
for m in installed_module_list.read():
f.write(m)
不过这样显然麻烦了,还有更简单的方法:
$ pip freeze >requirements.txt
这样就直接将信息导出到文件中了
requirements.txt文件内容如下:
在另一台服务器上想部署相同的包,只需运行:
$ pip install -r requirements.txt
来源:https://blog.csdn.net/fzp95/article/details/106516460
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python中Numpy和Matplotlib的基本使用指南
![](https://img.aspxhome.com/file/2023/6/112926_0s.png)
配置SQL Server以允许远程连接
python实现将中文日期转换为数字日期
![](https://img.aspxhome.com/file/2023/2/105252_0s.jpg)
MySQL的存储引擎InnoDB和MyISAM
![](https://img.aspxhome.com/file/2023/3/103563_0s.webp)
浅析Go汇编语法和MatrixOne使用介绍
Python中docx2txt库的使用说明
SQL Group By分组后如何选取每组最新的一条数据
![](https://img.aspxhome.com/file/2023/7/121237_0s.png)
python 如何获取文件夹中的全部文件
![](https://img.aspxhome.com/file/2023/6/83966_0s.png)
用python删除文件夹中的重复图片(图片去重)
Mac 安装 Python3.10 和 配置环境的详细教程
![](https://img.aspxhome.com/file/2023/2/115122_0s.jpg)
Perl中的符号 ->;、=>; 和 :: 分别表示什么意思?
详解Django中CSRF和CORS的区别
![](https://img.aspxhome.com/file/2023/1/78741_0s.jpg)
Linux/UNIX和Window平台上安装Mysql
Python 可视化神器Plotly详解
![](https://img.aspxhome.com/file/2023/1/95421_0s.jpg)
如何避免mysql启动时错误及sock文件作用分析
Python中time库的使用(日期时间)
![](https://img.aspxhome.com/file/2023/9/121019_0s.png)
Golang Mutex 原理详细解析
![](https://img.aspxhome.com/file/2023/4/123614_0s.png)