pipreqs icon indicating copy to clipboard operation
pipreqs copied to clipboard

UnicodeDecodeError: 'utf-8' codec can't decode byte

Open tbrodbeck opened this issue 6 years ago • 35 comments

I just installed pipreqs for the first time. This is what it does:

$ pipreqs $(pwd)
Traceback (most recent call last):
  File "/Users/tillmann/dev/textSum/.direnv/python-3.8.3/bin/pipreqs", line 10, in <module>
    sys.exit(main())
  File "/Users/tillmann/dev/textSum/.direnv/python-3.8.3/lib/python3.8/site-packages/pipreqs/pipreqs.py", line 470, in main
    init(args)
  File "/Users/tillmann/dev/textSum/.direnv/python-3.8.3/lib/python3.8/site-packages/pipreqs/pipreqs.py", line 406, in init
    candidates = get_all_imports(input_path,
  File "/Users/tillmann/dev/textSum/.direnv/python-3.8.3/lib/python3.8/site-packages/pipreqs/pipreqs.py", line 122, in get_all_imports
    contents = f.read()
  File "/usr/local/bin/../Cellar/[email protected]/3.8.3_2/bin/../Frameworks/Python.framework/Versions/3.8/lib/python3.8/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb1 in position 81: invalid start byte

Am I doing something wrong?

$ neofetch           
                    'c.          tillmann@MacBook-Pro 
                 ,xNMM.          -------------------- 
               .OMMMMo           OS: macOS Catalina 10.15.5 19F101 x86_64 
               OMMM0,            Host: MacBookPro15,1 
     .;loddo:' loolloddol;.      Kernel: 19.5.0 
   cKMMMMMMMMMMNWMMMMMMMMMM0:    Uptime: 17 days, 14 hours, 5 mins 
 .KMMMMMMMMMMMMMMMMMMMMMMMWd.    Packages: 115 (brew) 
 XMMMMMMMMMMMMMMMMMMMMMMMX.      Shell: zsh 5.7.1 
;MMMMMMMMMMMMMMMMMMMMMMMM:       Resolution: 1680x1050@2x, 3840x1600@2x, 1050x1680@2x 
:MMMMMMMMMMMMMMMMMMMMMMMM:       DE: Aqua 
.MMMMMMMMMMMMMMMMMMMMMMMMX.      WM: Quartz Compositor 
 kMMMMMMMMMMMMMMMMMMMMMMMMWd.    WM Theme: Blue (Light) 
 .XMMMMMMMMMMMMMMMMMMMMMMMMMMk   Terminal: Apple_Terminal 
  .XMMMMMMMMMMMMMMMMMMMMMMMMK.   Terminal Font: HackNerdFontComplete-Regular 
    kMMMMMMMMMMMMMMMMMMMMMMd     CPU: Intel i7-8850H (12) @ 2.60GHz 
     ;KMMMMMMMWXXWMMMMMMMk.      GPU: Intel UHD Graphics 630, Radeon Pro 560X 
       .cooc,.    .,coo:.        Memory: 10080MiB / 16384MiB 
$ python -V                      
Python 3.8.3

tbrodbeck avatar Jul 16 '20 08:07 tbrodbeck

$ pipreqs --encoding utf-8

Teraskull avatar Jul 24 '20 00:07 Teraskull

It is the same:

$ pipreqs --encoding utf-8
Traceback (most recent call last):
  File "/Users/tillmann/dev/textSum/.direnv/python-3.8.3/bin/pipreqs", line 10, in <module>
    sys.exit(main())
  File "/Users/tillmann/dev/textSum/.direnv/python-3.8.3/lib/python3.8/site-packages/pipreqs/pipreqs.py", line 470, in main
    init(args)
  File "/Users/tillmann/dev/textSum/.direnv/python-3.8.3/lib/python3.8/site-packages/pipreqs/pipreqs.py", line 406, in init
    candidates = get_all_imports(input_path,
  File "/Users/tillmann/dev/textSum/.direnv/python-3.8.3/lib/python3.8/site-packages/pipreqs/pipreqs.py", line 122, in get_all_imports
    contents = f.read()
  File "/usr/local/bin/../Cellar/[email protected]/3.8.3_2/bin/../Frameworks/Python.framework/Versions/3.8/lib/python3.8/codecs.py", line 322, in decode
    (result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb1 in position 81: invalid start byte

tbrodbeck avatar Jul 24 '20 09:07 tbrodbeck

Same error here $ pipreqs --encoding utf-8 --print /home/kamalmehta/developments/alphaslate Traceback (most recent call last): .... (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb1 in position 81: invalid start byte

kamalmeh avatar Aug 12 '20 09:08 kamalmeh

@kamalmeh, try this:

$ pipreqs --encoding=iso-8859-1

Teraskull avatar Aug 12 '20 11:08 Teraskull

I had a .venv directory with the usual hugh collection of python files. Something in the package black.py messed it up. A rm -rf .venv, and "pipreqs ." worked.

dougsweetser avatar Nov 11 '20 01:11 dougsweetser

This happens when certain modules/packages got files which are not encoded with the provided encoding (e.g. UTF-8 vs. UTF-8 with BOM). In my case IPython has a file called nonascii.py which was encoded with ISO-8859-5 (& got Cyrillic characters) and joblib got a file (test_func_inspect_special_encoding.py) which was encoded with Big5 (seems to be a Chinese encoding) and also had Chinese characters inside it, as well as srsly got a file called test_ujson.py which was encoded with UTF-8 BOM despite specifying UTF-8 in its header.

I solved my issue by commenting out the first two files and saving the last one in the correct encoding. To circumvent such error pipreqs maybe should try to determine the encoding of each file dynamically be either reading the encoding specified in the header of the file or somehow determine the encoding which most likely has been used.

phHartl avatar Mar 01 '21 13:03 phHartl

$ pipreqs --encoding utf-8

It works, but why not set utf-8 as default in source code, so we do not have to add that params?

magictomagic avatar Mar 17 '21 18:03 magictomagic

It seems to me that the issue originally reported here has been solved, the user had a file in a different encoding that they didn't know about, so I will be closing this issue. I will accept a PR if anyone has a good idea on how to reliably decide the file encoding, but until then I will just make utf-8 the default encoding used by pipreqs

alan-barzilay avatar May 09 '21 04:05 alan-barzilay

I had a .venv directory with the usual hugh collection of python files. Something in the package black.py messed it up. A rm -rf .venv, and "pipreqs ." worked.

Thank you sir - this finally fixed my issue

jeremydiba avatar Feb 28 '22 19:02 jeremydiba

I had a .venv directory with the usual hugh collection of python files. Something in the package black.py messed it up. A rm -rf .venv, and "pipreqs ." worked.

Thank you sir - this finally fixed my issue

Adding ignore parameters also work fine and you don't have to remove virtual env!

pipreqs . --ignore ".env"

AgaMiko avatar Apr 25 '22 07:04 AgaMiko

I had a .venv directory with the usual hugh collection of python files. Something in the package black.py messed it up. A rm -rf .venv, and "pipreqs ." worked.

Thank you sir - this finally fixed my issue

Adding ignore parameters also work fine and you don't have to remove virtual env!

pipreqs . --ignore ".env"

Thank you. This worked for me. There was no need to specify the encoding using the --encoding flag.

❯ python3 --version
Python 3.8.10
❯ pipreqs --version
0.4.11

scotgopal avatar Aug 12 '22 03:08 scotgopal

Hi,

I'm having a similar problem . How can i fix it? Thank you

(VSCodeENV2) C:\Users\emora\Desktop\VSCodeENV2>pipreqs --encoding utf-8 Traceback (most recent call last): File "C:\Users\emora\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 196, in _run_module_as_main return _run_code(code, main_globals, None, File "C:\Users\emora\AppData\Local\Programs\Python\Python310\lib\runpy.py", line 86, in run_code exec(code, run_globals) File "C:\Users\emora\Desktop\VSCodeENV2\VSCodeENV2\Scripts\pipreqs.exe_main.py", line 7, in File "C:\Users\emora\Desktop\VSCodeENV2\VSCodeENV2\lib\site-packages\pipreqs\pipreqs.py", line 488, in main init(args) File "C:\Users\emora\Desktop\VSCodeENV2\VSCodeENV2\lib\site-packages\pipreqs\pipreqs.py", line 415, in init candidates = get_all_imports(input_path, File "C:\Users\emora\Desktop\VSCodeENV2\VSCodeENV2\lib\site-packages\pipreqs\pipreqs.py", line 115, in get_all_imports contents = f.read() File "C:\Users\emora\AppData\Local\Programs\Python\Python310\lib\codecs.py", line 322, in decode (result, consumed) = self._buffer_decode(data, self.errors, final) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb1 in position 81: invalid start byte

efrenmo avatar Oct 19 '22 04:10 efrenmo

I had the similar problem. The way I how I fixed is by deleting all the temporary python files in the folder. rm -f .*py *.swp

This deletes all the temporary python scripts. pipreqs --encoding utf-8 path_to_folder/

ankurk017 avatar Nov 28 '22 15:11 ankurk017

Hello everyone!

As for me the issue is still present for some of you. Here is my "solution":

As @phHartl mentioned, you can skip conflicting files (nonascii.py and test_func_inspect_special_encoding.py in my case) by doing this:

In the python file of your venv (lib64/python3.9/site-packages/pipreqs/pipreqs.py), make your code similar to this snippet (on time of writting this, line is 114)

with open(file_name, "r", encoding=encoding) as f:
     print(file_name)
     if file_name in ['...../nonascii.py', ...../'test_func_inspect_special_encoding.py']:
          continue
     contents = f.read() 

I agree it is not an elegant solution, but it works!

Hope, you find it useful.

beybars1 avatar Mar 15 '23 13:03 beybars1

Along the lines of beybars1 did but with less round-tripping:

replace contents = f.read() around line 115 in pipreqs.py with

try:
    contents = f.read()
except UnicodeDecodeError as ude:
    print(f'UnicodeDecodeError reading {file_name} (couldn\'t read contents). Skipping.')

mheyman avatar Mar 23 '23 15:03 mheyman

Hey.

I had a .venv directory with the usual hugh collection of python files. Something in the package black.py messed it up. A rm -rf .venv, and "pipreqs ." worked.

Thank you sir - this finally fixed my issue

Adding ignore parameters also work fine and you don't have to remove virtual env!

pipreqs . --ignore ".env"

Thanks, this worked for me too. Just to add some context, this applies if you've labeled your virtual env and "env". If its named "venv" then replace the ".env" with ".venv"

mbkimani avatar Apr 05 '23 09:04 mbkimani

@kamalmeh, try this:

$ pipreqs --encoding=iso-8859-1

saved the day

conkeur avatar Sep 12 '23 02:09 conkeur

unfortunately this error still exists

pipreqs UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 301: character maps to

pipreqs --encoding=utf8 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfc in position 224: invalid start byte

pipreqs --encoding=iso-8859-1 SyntaxError: invalid character '¼' (U+00BC)

python -V Python 3.10.13

pip -V pip 23.2.1

pip packages: only fastai+uvicorn

csakaszamok avatar Sep 28 '23 06:09 csakaszamok

This issue still exists

hemjeet avatar Nov 04 '23 07:11 hemjeet

For me too.

FlorianWilhelm avatar Nov 20 '23 15:11 FlorianWilhelm