问题描述
想使用python的pipreqs命令生成项目所需的依赖,执行报出编码错误
C:\Projects\dc>pipreqs .
Traceback (most recent call last):
File "c:\coding\python37\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "c:\coding\python37\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Coding\Python37\Scripts\pipreqs.exe\__main__.py", line 9, in <module>
File "c:\coding\python37\lib\site-packages\pipreqs\pipreqs.py", line 396, in main
init(args)
File "c:\coding\python37\lib\site-packages\pipreqs\pipreqs.py", line 341, in init
extra_ignore_dirs=extra_ignore_dirs)
File "c:\coding\python37\lib\site-packages\pipreqs\pipreqs.py", line 75, in get_all_imports
contents = f.read()
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 78: illegal multibyte sequence
网上给的解决方案基本上都是在open文件时,加上rb参数
path = 'D://Document And Settings3/lqz/Desktop/Walden.txt'
file = open(path, 'rb')
print(file.read())
但是我执行的是一个命令,不是打开文件
解决方案
后来经过摸索,尝试在命令后面加上了--encoding参数,居然瞎猫碰上死耗子,给蒙对了,具体如下:
C:\Projects\dc>pipreqs . --encoding=utf-8 --force
INFO: Successfully saved requirements file in .\requirements.txt
大胆猜想,小心求证! -----沃兹基硕德