有时候会创建多个python环境,习惯性的使用pipenv,结果之前用的好好的pipenv突然报编码错误。emmmmm…只能说诡异。
python 3.6.6、pipenv 2018.11.26
运行“pipenv shell”创建环境,结果报错了。
Failed creating virtual environment
[pipenv.exceptions.VirtualenvCreationException]: File "e:\python36\lib\site-packages\pipenv\cli\command.py", line 390, in shell
[pipenv.exceptions.VirtualenvCreationException]: pypi_mirror=state.pypi_mirror,
[pipenv.exceptions.VirtualenvCreationException]: File "e:\python36\lib\site-packages\pipenv\core.py", line 2156, in do_shell
[pipenv.exceptions.VirtualenvCreationException]: three=three, python=python, validate=False, pypi_mirror=pypi_mirror,
[pipenv.exceptions.VirtualenvCreationException]: File "e:\python36\lib\site-packages\pipenv\core.py", line 574, in ensure_project
[pipenv.exceptions.VirtualenvCreationException]: pypi_mirror=pypi_mirror,
[pipenv.exceptions.VirtualenvCreationException]: File "e:\python36\lib\site-packages\pipenv\core.py", line 506, in ensure_virtualenv
[pipenv.exceptions.VirtualenvCreationException]: python=python, site_packages=site_packages, pypi_mirror=pypi_mirror
[pipenv.exceptions.VirtualenvCreationException]: File "e:\python36\lib\site-packages\pipenv\core.py", line 935, in do_create_virtualenv
[pipenv.exceptions.VirtualenvCreationException]: extra=[crayons.blue("{0}".format(c.err)),]
[pipenv.exceptions.VirtualenvCreationException]: Traceback (most recent call last):
File "e:\python36\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "e:\python36\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "e:\python36\lib\site-packages\virtualenv.py", line 2345, in
main()
File "e:\python36\lib\site-packages\virtualenv.py", line 712, in main
symlink=options.symlink)
File "e:\python36\lib\site-packages\virtualenv.py", line 947, in create_environment
download=download,
File "e:\python36\lib\site-packages\virtualenv.py", line 904, in install_wheel
call_subprocess(cmd, show_stdout=False, extra_env=env, stdin=SCRIPT)
File "e:\python36\lib\site-packages\virtualenv.py", line 770, in call_subprocess
line = line.decode(encoding)
AttributeError: 'str' object has no attribute 'decode'
Failed to create virtual environment.
网上找了一圈也没找到有人说过这个错误,难道是我查找的姿势不对??
看起来好像是编码的问题,打开文件"e:\python36\lib\site-packages\virtualenv.py"找到报错位置,修改代码:
proc_stdout = proc_stdout.strip().decode("utf-8")
改成:
proc_stdout = proc_stdout.strip() if type(proc_stdout) is str else proc_stdout.strip().decode("utf-8")
改完运行,又报错:
File "e:\python36\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "e:\python36\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "e:\python36\lib\site-packages\virtualenv.py", line 2347, in
main()
File "e:\python36\lib\site-packages\virtualenv.py", line 712, in main
symlink=options.symlink)
File "e:\python36\lib\site-packages\virtualenv.py", line 949, in create_environment
download=download,
File "e:\python36\lib\site-packages\virtualenv.py", line 906, in install_wheel
call_subprocess(cmd, show_stdout=False, extra_env=env, stdin=SCRIPT)
File "e:\python36\lib\site-packages\virtualenv.py", line 763, in call_subprocess
proc.stdin.write(stdin)
TypeError: write() argument must be str, not bytes
再根据错误改:
proc.stdin.write(stdin)
在这行代码上边加上一行:
stdin = stdin if type(stdin) is str else bytes.decode(stdin,encoding='utf8')
proc.stdin.write(stdin)
运行,又报错。没事,微笑面对。
File "e:\python36\lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "e:\python36\lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "e:\python36\lib\site-packages\virtualenv.py", line 2345, in
main()
File "e:\python36\lib\site-packages\virtualenv.py", line 712, in main
symlink=options.symlink)
File "e:\python36\lib\site-packages\virtualenv.py", line 947, in create_environment
download=download,
File "e:\python36\lib\site-packages\virtualenv.py", line 904, in install_wheel
call_subprocess(cmd, show_stdout=False, extra_env=env, stdin=SCRIPT)
File "e:\python36\lib\site-packages\virtualenv.py", line 770, in call_subprocess
line = line.decode(encoding)
AttributeError: 'str' object has no attribute 'decode'
我再改!!继续找到这行代码。
line = line.decode(encoding)
改成:
line = line.decode(encoding) if type(line) is not str else line
改完以后再次运行。
成功。
按理来说之前能成功运行,突然创建不了,又是编码的问题,难道是因为我电脑有改动过什么编码??有时间再瞅瞅看能不能找到原因吧。