UnicodeDecodeError: 'utf-8' codec can't decode byte
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
$ pipreqs --encoding utf-8
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
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, try this:
$ pipreqs --encoding=iso-8859-1
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.
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.
$ 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?
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
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
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"
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
ignoreparameters 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
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
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/
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.
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.')
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
ignoreparameters 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"
@kamalmeh, try this:
$ pipreqs --encoding=iso-8859-1
saved the day
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
This issue still exists
For me too.