解决 PyCharm 设置 pipenv 报错的问题

解决 PyCharm 设置 pipenv 报错的问题

使用 PyCharm 添加 pipenv 会报如下错误:

Executed command:
/usr/local/bin/pipenv --python /usr/local/bin/python3.6 install --dev

Error occurred:
Error Running Pipenv

Command output:
Traceback (most recent call last):
  File "/usr/local/Cellar/pipenv/2018.11.26_2/libexec/bin/pipenv", line 6, in <module>
    from pkg_resources import load_entry_point
  File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pkg_resources/__init__.py", line 3241, in <module>
    @_call_aside
  File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pkg_resources/__init__.py", line 3225, in _call_aside
    f(*args, **kwargs)
  File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pkg_resources/__init__.py", line 3254, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pkg_resources/__init__.py", line 583, in _build_master
    ws.require(__requires__)
  File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pkg_resources/__init__.py", line 900, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pkg_resources/__init__.py", line 786, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'pipenv==2018.11.26' distribution was not found and is required by the application

QQ20190707-161418@2x

这个问题是因为系统同时存在 Python 3.6 和 Python 3.7 导致的,查看了下,Homebrew 安装的 Python 3.7,而通过官网的 dmg 安装的 Python 3.6。我觉得这个与 PyCharm 内部的运行环境有关,即使我默认的已经是 Python 3.6 了,它不知为何依然调用了 Python 3.7 来执行。尝试卸掉了一个,发现这个问题解决了。

再次尝试,报了另外的错:

Executed command:
/Users/bean/Library/Python/3.6/bin/pipenv --python /usr/local/bin/python3.6 install --dev

Error occurred:
RuntimeError: Click will abort further execution because Python 3 was configured to use ASCII as encoding for the environment. Consult https://click.palletsprojects.com/en/7.x/python3/ for mitigation steps.


Traceback (most recent call last):
  File "/Users/bean/Library/Python/3.6/bin/pipenv", line 10, in <module>
    sys.exit(cli())
  File "/Users/bean/Library/Python/3.6/lib/python/site-packages/pipenv/vendor/click/core.py", line 764, in __call__
    return self.main(*args, **kwargs)
  File "/Users/bean/Library/Python/3.6/lib/python/site-packages/pipenv/vendor/click/core.py", line 696, in main
    _verify_python3_env()
  File "/Users/bean/Library/Python/3.6/lib/python/site-packages/pipenv/vendor/click/_unicodefun.py", line 124, in _verify_python3_env
    ' mitigation steps.' + extra
RuntimeError: Click will abort further execution because Python 3 was configured to use ASCII as encoding for the environment. Consult https://click.palletsprojects.com/en/7.x/python3/ for mitigation steps.

This system lists a couple of UTF-8 supporting locales that
you can pick from.  The following suitable locales were
discovered: af_ZA.UTF-8, am_ET.UTF-8, be_BY.UTF-8, bg_BG.UTF-8, ca_ES.UTF-8, cs_CZ.UTF-8, da_DK.UTF-8, de_AT.UTF-8, de_CH.UTF-8, de_DE.UTF-8, el_GR.UTF-8, en_AU.UTF-8, en_CA.UTF-8, en_GB.UTF-8, en_IE.UTF-8, en_NZ.UTF-8, en_US.UTF-8, es_ES.UTF-8, et_EE.UTF-8, eu_ES.UTF-8, fi_FI.UTF-8, fr_BE.UTF-8, fr_CA.UTF-8, fr_CH.UTF-8, fr_FR.UTF-8, he_IL.UTF-8, hr_HR.UTF-8, hu_HU.UTF-8, hy_AM.UTF-8, is_IS.UTF-8, it_CH.UTF-8, it_IT.UTF-8, ja_JP.UTF-8, kk_KZ.UTF-8, ko_KR.UTF-8, lt_LT.UTF-8, nl_BE.UTF-8, nl_NL.UTF-8, no_NO.UTF-8, pl_PL.UTF-8, pt_BR.UTF-8, pt_PT.UTF-8, ro_RO.UTF-8, ru_RU.UTF-8, sk_SK.UTF-8, sl_SI.UTF-8, sr_YU.UTF-8, sv_SE.UTF-8, tr_TR.UTF-8, uk_UA.UTF-8, zh_CN.UTF-8, zh_HK.UTF-8, zh_TW.UTF-8

Google 了半天,没找到有效的答案,大多数是让重新安装之类的,没意义。后来在 JetBrains 的 Issues 平台上提交了问题,最终发现这是一个已知问题,并且与 mac 上使用了 zsh 终端相关,可能 oh-my-zsh 重置了编码,导致 PyCharm 的运行环境中存在问题,最终解决方法如下:

.zshrc 文件最后指定编码:

export LANG=zh_CN.UTF-8

重启 PyCharm,问题解决!