pipreqs: 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/python@3.8/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
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 27
Adding
ignoreparameters also work fine and you don’t have to remove virtual env!pipreqs . --ignore ".env"@kamalmeh, try this:
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
IPythonhas a file callednonascii.pywhich was encoded with ISO-8859-5 (& got Cyrillic characters) andjoblibgot 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 assrslygot a file calledtest_ujson.pywhich 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.
As it was the whole environment folder that was causing the problem, what worked for me is:
pipreqs . --ignore <path_to_env_folder> --force
it’s working @mervess
Hey.
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”
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 *.swpThis deletes all the temporary python scripts.
pipreqs --encoding utf-8 path_to_folder/Along the lines of beybars1 did but with less round-tripping:
replace
contents = f.read()around line 115 inpipreqs.pywithHello 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.pyandtest_func_inspect_special_encoding.pyin 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)I agree it is not an elegant solution, but it works!
Hope, you find it useful.