pipreqs icon indicating copy to clipboard operation
pipreqs copied to clipboard

Unicode Decode Error

Open hadi-f90 opened this issue 11 months ago • 8 comments

Problem

In Python 3.12 running pipreqs returns this error:

<unknown>:165: SyntaxWarning: invalid escape sequence '\S'
<unknown>:166: SyntaxWarning: invalid escape sequence '\['
<unknown>:207: SyntaxWarning: invalid escape sequence '\['
<unknown>:456: SyntaxWarning: invalid escape sequence '\S'
<unknown>:200: SyntaxWarning: invalid escape sequence '\:'
Traceback (most recent call last):
  File "/*******/.local/bin/pipreqs", line 8, in <module>
    sys.exit(main())
             ^^^^^^
  File "/*******/.local/lib/python3.12/site-packages/pipreqs/pipreqs.py", line 609, in main
    init(args)
  File "/*******/.local/lib/python3.12/site-packages/pipreqs/pipreqs.py", line 533, in init
    candidates = get_all_imports(
                 ^^^^^^^^^^^^^^^^
  File "/*******/.local/lib/python3.12/site-packages/pipreqs/pipreqs.py", line 136, in get_all_imports
    contents = read_file_content(file_name, encoding)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/*******/.local/lib/python3.12/site-packages/pipreqs/pipreqs.py", line 181, in read_file_content
    contents = f.read()
               ^^^^^^^^
  File "<frozen codecs>", line 322, in decode
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb1 in position 81: invalid start byte

Therefore, it doesn't save any requirements.txt

Possible Solution

It seems that changing the afformentioned piece of code into this one can manage to solve it temporarily:

        with open(file_name, "r", encoding=encoding) as f:
            try:
                contents = f.read()
            except UnicodeDecodeError:
                contents = f.read().enode("utf-8")

Although changing this solves the main issue about saving requirements.txt file, it adds raising of a number of warnings about the name and need to check module/lib names from PyPi which slows down pipreqs.

PS: Other changes returned similar warnings

hadi-f90 avatar Jan 06 '25 16:01 hadi-f90

+1, same issue on Windows with python 3.13.1 on 0.4 versions of pipreqs too

pipreqs --force --encoding utf-8

(or)

pipreqs --force --encoding utf-8-sig

py563 avatar Jan 12 '25 09:01 py563

Nope, this doesn't work

janekolszak avatar Jan 30 '25 20:01 janekolszak

I have the same problem

JvdsReform avatar Jan 31 '25 12:01 JvdsReform

Try ignoring the venv folder if you have one, for example

pipreqs --ignore .venv,venv

mzglinski avatar Jan 31 '25 12:01 mzglinski

@mzglinski That worked, thanks!!

Jxlle avatar Jan 31 '25 19:01 Jxlle

I also get this error, setting the encoding and ignoring the venv Folder doesn't work.

HerrFrodo avatar Mar 25 '25 17:03 HerrFrodo

Adding that ignoring my virtual environment solved the exact same error.

lonswaymar avatar May 07 '25 18:05 lonswaymar

Try ignoring the venv folder if you have one, for example

pipreqs --ignore .venv,venv

THANKS 👍

sbigstar0310 avatar Jun 14 '25 05:06 sbigstar0310