python项目依赖库生成requirements.txt文件
第一种:pip freeze > requirements.txt
第二种:pipreqs 项目根目录(不能有中文)--encoding=utf8
requirements已存在: pipreqs 项目根目录 --encoding=utf8 --force
第一种:pip freeze > requirements.txt
步骤:
cd 到你的项目根目录
-
执行:pip freeze > requirements.txt
C:\Users\dyunren>i: I:\>cd I:\python20\softwaredate\py_basics
缺点:`你本机python环境所有依赖都会写到 requirements.txt文件当中。
如图展示:
第二种:pipreqs
pipreqs的安装:
pip install pipreqs
查看版本:
pip show pipreqs 或 pipreqs --version
使用:pipreqs python项目根目录
C:\Users\dyunren>i:
I:\>cd I:\python20\softwaredate\py_basics
I:\python20\softwaredate\py_basics>pipreqs
Traceback (most recent call last):
File "d:\software\python37\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "d:\software\python37\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "D:\software\python37\Scripts\pipreqs.exe\__main__.py", line 9, in <modul
e>
File "d:\software\python37\lib\site-packages\pipreqs\pipreqs.py", line 470, in
main
init(args)
File "d:\software\python37\lib\site-packages\pipreqs\pipreqs.py", line 409, in
init
follow_links=follow_links)
File "d:\software\python37\lib\site-packages\pipreqs\pipreqs.py", line 122, in
get_all_imports
contents = f.read()
UnicodeDecodeError: 'gbk' codec can't decode byte 0xaf in position 207: illegal
multibyte sequence
- 如上图出现了编码出错'gbk',需要转换成utf-8
pipreqs I:\python20\softwaredate\py_basics --encoding=utf8
ERROR: Failed on file: I:\python20\softwaredate\py_basics\列表操作\列表序列通用
操作.py
Traceback (most recent call last):
File "d:\software\python37\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "d:\software\python37\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "D:\software\python37\Scripts\pipreqs.exe\__main__.py", line 9, in <modul
e>
File "d:\software\python37\lib\site-packages\pipreqs\pipreqs.py", line 470, in
main
init(args)
File "d:\software\python37\lib\site-packages\pipreqs\pipreqs.py", line 409, in
init
follow_links=follow_links)
File "d:\software\python37\lib\site-packages\pipreqs\pipreqs.py", line 138, in
get_all_imports
raise exc
File "d:\software\python37\lib\site-packages\pipreqs\pipreqs.py", line 124, in
get_all_imports
tree = ast.parse(contents)
File "d:\software\python37\lib\ast.py", line 35, in parse
return compile(source, filename, mode, PyCF_ONLY_AST)
File "<unknown>", line 1
# -*- coding: utf-8 -*-
^
SyntaxError: invalid character in identifier
报错为标识符错误,是因为我文件名有带中文了。真的是坑啊,只好换个项目试一下了。
- pipreqs D:\softwaredate\TestAPI --encoding=utf8
如果报requirements.tx存在的警告信息:
- 可以使用--force 参数来覆盖
C:\Users\dyunren>pipreqs D:\softwaredate\TestAPI --encoding=utf8
WARNING: Requirements.txt already exists, use --force to overwrite it
pipreqs D:\softwaredate\TestAPI --encoding=utf8 --force
requirements.txt中的依赖如何安装:
pip install -r requirements.txt