Neither 'pyenv' nor 'asdf' could be found to install Python

文章目录

    背景

    系统环境

    • Ubuntu 18.04
    • Python 3.6

    但是,这个 Python 项目在 pipenv 配置文件中指定的 python 版本是 3.8。

    所以,执行 pipenv shell 命令时,报错:

    $ pipenv shell
    
    Warning: Python 3.8 was not found on your system...
    Neither 'pyenv' nor 'asdf' could be found to install Python.
    You can specify specific versions of Python with:
    $ pipenv --python path/to/python
    

    pyenv 是用来管理多版本 Python 的一个工具。

    安装 pyenv

    参考 https://github.com/pyenv/pyenv

    例如我是 Ubuntu 18.04 + Fish shell

    > git clone https://github.com/pyenv/pyenv.git ~/.pyenv
    > set -Ux PYENV_ROOT $HOME/.pyenv
    > set -Ux fish_user_paths $PYENV_ROOT/bin $fish_user_paths
    

    至此,安装并配置成功 pyenv。

    自动安装指定的 Python 版本

    再次执行

    > pipenv shell
    
    Warning: Python 3.8 was not found on your system...
    Would you like us to install CPython 3.8.6 with Pyenv? [Y/n]: Y
    Installing CPython 3.8.6 with /home/zhongwei/.pyenv/bin/pyenv (this may take a few minutes)...
    

    会看到 pipenv 调用 pyenv 自动安装缺失的 Python 版本。

    但是,pyenv 下载太慢了

    由于 pyenv 默认从国外服务器下载,下载过程漫长,而且没有进度提示。无法忍受,可以直接从国内镜像下载对应 python 版本到 pyenv 的 cache 目录,然后手动安装。

    $ version="3.8.6"; echo $version; wget "https://mirrors.huaweicloud.com/python/$version/Python-$version.tar.xz" -P ~/.pyenv/cache/;pyenv install $version
    

    报错 zipimport.ZipImportError: can’t decompress data; zlib not available

    sudo apt-get install zlib1g-dev
    

    参考

    • https://stackoverflow.com/questions/49794432/how-to-setup-a-pipenv-python-3-6-project-if-os-python-version-is-3-5
    • https://github.com/pyenv/pyenv

    关于作者 🌱

    我是来自山东烟台的一名开发者,有感兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊,或者关注我的个人公众号“大象工具”, 查看更多联系方式