【pipreqs编码问题】使用pipreqs导出当前项目所需要的的依赖,出现的编码问题

Project description

Installation

pip install pipreqs

Usage

Usage:
    pipreqs [options] <path>

Options:
    --use-local           Use ONLY local package info instead of querying PyPI
    --pypi-server <url>   Use custom PyPi server
    --proxy <url>         Use Proxy, parameter will be passed to requests library. You can also just set the
                          environments parameter in your terminal:
                          $ export HTTP_PROXY="http://10.10.1.10:3128"
                          $ export HTTPS_PROXY="https://10.10.1.10:1080"
    --debug               Print debug information
    --ignore <dirs>...    Ignore extra directories
    --encoding <charset>  Use encoding parameter for file open
    --savepath <file>     Save the list of requirements in the given file
    --print               Output the list of requirements in the standard output
    --force               Overwrite existing requirements.txt
    --diff <file>         Compare modules in requirements.txt to project imports.
    --clean <file>        Clean up requirements.txt by removing modules that are not imported in project.
    --no-pin              Omit version of output packages.

Example

$ pipreqs /home/project/location
Successfully saved requirements file in /home/project/location/requirements.txt

Contents of requirements.txt

wheel==0.23.0
Yarg==0.1.9
docopt==0.6.2

Why not pip freeze?

  • pip freeze only saves the packages that are installed with pip install in your environment.
  • pip freeze saves all packages in the environment including those that you don’t use in your current project. (if you don’t have virtualenv)
  • and sometimes you just need to create requirements.txt for a new project without installing modules.

以上描述来自官方文档,根据官方描述安装后,在使用 pipreqs ./ 指令导出依赖的过程中,很多人遇见了以下编码问题:

Traceback (most recent call last):
  File "d:\python37\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "d:\python37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "D:\Python37\Scripts\pipreqs.exe\__main__.py", line 7, in <module>
  File "d:\python37\lib\site-packages\pipreqs\pipreqs.py", line 470, in main
    init(args)
  File "d:\python37\lib\site-packages\pipreqs\pipreqs.py", line 409, in init
    follow_links=follow_links)
  File "d:\python37\lib\site-packages\pipreqs\pipreqs.py", line 122, in get_all_imports
    contents = f.read()
UnicodeDecodeError: 'gbk' codec can't decode byte 0xa6 in position 186: illegal multibyte sequence

查看了很多篇解决这个问题的文章,大都是按以下方式处理:

若出现类似上边的报错UnicodeDecodeError: 'gbk' codec can't decode byte 0xa6 in position 186: illegal multibyte sequence 
直接修改pipreqs.py 的75行,将encoding改为'utf-8'

 然鹅,按上面方式处理后,虽不报GBK编码的问题了,但报了 utf-8 的编码问题。。

Traceback (most recent call last):
  File "d:\python37\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "d:\python37\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "D:\Python37\Scripts\pipreqs.exe\__main__.py", line 7, in <module>
  File "d:\python37\lib\site-packages\pipreqs\pipreqs.py", line 470, in main
    init(args)
  File "d:\python37\lib\site-packages\pipreqs\pipreqs.py", line 409, in init
    follow_links=follow_links)
  File "d:\python37\lib\site-packages\pipreqs\pipreqs.py", line 122, in get_all_imports
    contents = f.read()
  File "d:\python37\lib\codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb4 in position 1747: invalid start byte

后来,我去了pypi官网,试图找到一个解决此问题的民间版本,发现有一个名为 pipreqs-zh 的包,有点惊喜,看这名字,不会就是解决这个中文编码的问题的吧。就试着安装执行了一下,还是没有作用。

最终解决

使用ISO-8859-1 编码,

pipreqs ./ --encoding='iso-8859-1'

INFO: Successfully saved requirements file in ./requirements.txt
 

 

上一篇:(转载) 安装nltk遇到的坑:下载失败+安装成功但导入不了


下一篇:2021-02-20