如何在 Mac 上使用 pipenv? [英] How to use pipenv on mac?

查看:67
本文介绍了如何在 Mac 上使用 pipenv?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过pip(pip install pipenv)安装时,在zsh shell上找不到pipenv命令.

如果通过brew安装:brew install pipenv,然后运行pipenv shell,报错

正在加载 .env 环境变量...在虚拟环境中启动子shell...回溯(最近一次调用最后一次):文件/usr/local/Cellar/pipenv/2020.11.15/libexec/bin/pipenv",第8行,在<module>sys.exit(cli())文件/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/vendor/click/core.py",第829行,__call__返回 self.main(*args, **kwargs)文件/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/vendor/click/core.py",第782行,主目录rv = self.invoke(ctx)文件/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/vendor/click/core.py",第1259行,invoke返回_process_result(sub_ctx.command.invoke(sub_ctx))文件/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/vendor/click/core.py",第1066行,invoke返回 ctx.invoke(self.callback, **ctx.params)文件/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/vendor/click/core.py",第610行,invoke返回回调(*args,**kwargs)文件/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/vendor/click/decorators.py",第73行,new_func返回 ctx.invoke(f, obj, *args, **kwargs)文件/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/vendor/click/core.py",第610行,invoke返回回调(*args,**kwargs)文件/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/cli/command.py",第429行,在shell中do_shell(文件/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/core.py",第2387行,在do_shell中shell.fork_compat(*fork_args)文件/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/shells.py",第106行,fork_compatc = pexpect.spawn(self.cmd, [-i"], 维度=(dims.lines, dims.columns))文件/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/vendor/pexpect/pty_spawn.py",第205行,在__init__self._spawn(命令,参数,preexec_fn,维度)文件/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/vendor/pexpect/pty_spawn.py",第276行,在_spawnraise ExceptionPexpect('该命令未找到或不是 ' +pipenv.vendor.pexpect.exceptions.ExceptionPexpect:命令未找到或不可执行:/use/bin/zsh.

没有命名 /use/bin/zsh 的路径.为什么不稳定?

shell 路径为

echo $SHELL/bin/zsh

解决方案

你在问两个问题,真的.我将在下面的单独部分中回答每个问题:

如何修复该错误

<块引用>

正在加载 .env 环境变量......该命令未找到或不可执行:/use/bin/zsh.

看起来像 在您的 .env 文件,你有 PIPENV_SHELL=/use/bin/zsh.那是不正确的.相反,它应该是

PIPENV_SHELL=/bin/zsh

甚至干脆

PIPENV_SHELL=zsh

或者您也可以将其删除.然后 pipenv shell 将简单地使用与您从中调用它相同的 shell.

如何在 macOS 上正确安装 pipenv

在 macOS 上安装 pipenv 的正确方法有点复杂,但如果您不想在升级 Python 版本时遇到问题,这是唯一的方法:

  1. 撤消您目前所做的操作:

    % pip uninstall pipenv% brew 卸载 pipenv

  2. 将以下内容添加到您的 .zprofile 文件中:

    eval "$( brew shellenv )"export PYENV_VERSION=3.9.5 # 设置你喜欢的 Python 版本.导出 PYENV_ROOT=~/.pyenv导出 PIPX_BIN_DIR=~/.local/binexport -U PATH path # -U 消除重复项路径=($PIPX_BIN_DIR$PYENV_ROOT/{bin,shims}$路径)

  3. 打开一个新的终端窗口(使上述更改生效)并执行以下操作:

    % brew install pyenv% pyenv install $PYENV_VERSION # 安装你喜欢的 Python.% pyenv global $PYENV_VERSION # 使它成为你的默认 Python.% pip install -U pip # 更新 pip.% pip install -U --user pipx # 安装 pipx 到 ~/.local/bin% pipx install pipenv # pipx 类似于 brew 但适用于 Python.

  4. 将以下内容添加到您的 .zshrc 文件中:

    eval "$( pyenv init - )";eval "$( pip completion --zsh )";eval "$( register-python-argcomplete pipx )";eval "$( pipenv --completion )";

  5. 重新启动您的 shell(通过键入 exec zsh 或打开一个新的终端窗口)以使上述内容生效.

When install it by pip(pip install pipenv), on zsh shell can't find the command pipenv.

If install it by brew: brew install pipenv, then run pipenv shell, got error

Loading .env environment variables...
Launching subshell in virtual environment...
Traceback (most recent call last):
  File "/usr/local/Cellar/pipenv/2020.11.15/libexec/bin/pipenv", line 8, in <module>
    sys.exit(cli())
  File "/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/vendor/click/core.py", line 829, in __call__
    return self.main(*args, **kwargs)
  File "/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/vendor/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/vendor/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/vendor/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/vendor/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/vendor/click/decorators.py", line 73, in new_func
    return ctx.invoke(f, obj, *args, **kwargs)
  File "/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/vendor/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/cli/command.py", line 429, in shell
    do_shell(
  File "/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/core.py", line 2387, in do_shell
    shell.fork_compat(*fork_args)
  File "/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/shells.py", line 106, in fork_compat
    c = pexpect.spawn(self.cmd, ["-i"], dimensions=(dims.lines, dims.columns))
  File "/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/vendor/pexpect/pty_spawn.py", line 205, in __init__
    self._spawn(command, args, preexec_fn, dimensions)
  File "/usr/local/Cellar/pipenv/2020.11.15/libexec/lib/python3.9/site-packages/pipenv/vendor/pexpect/pty_spawn.py", line 276, in _spawn
    raise ExceptionPexpect('The command was not found or was not ' +
pipenv.vendor.pexpect.exceptions.ExceptionPexpect: The command was not found or was not executable: /use/bin/zsh.

There isn't a path naming /use/bin/zsh. Why it's unstable?

The shell path is

echo $SHELL
/bin/zsh

解决方案

You're asking two questions, really. I'll answer each in a separate section below:

How to fix that error

Loading .env environment variables...
...
The command was not found or was not executable: /use/bin/zsh.

Looks like in your .env file, you have PIPENV_SHELL=/use/bin/zsh. That's incorrect. Instead, it should be

PIPENV_SHELL=/bin/zsh

or even simply

PIPENV_SHELL=zsh

Or you can also just remove it. Then pipenv shell will simply use the same shell as from which you invoke it.

How to properly install pipenv on macOS

The right way to install pipenv on macOS is a bit convoluted, but it's the only way to go if you don't want to run into problems whenever you upgrade your Python version:

  1. Undo what you've done so far:

    % pip uninstall pipenv
    % brew uninstall pipenv
    

  2. Add the following to your .zprofile file:

    eval "$( brew shellenv )"
    export PYENV_VERSION=3.9.5  # Set your preferred Python version.
    export PYENV_ROOT=~/.pyenv
    export PIPX_BIN_DIR=~/.local/bin
    export -U PATH path         # -U eliminates duplicates
    path=( 
        $PIPX_BIN_DIR
        $PYENV_ROOT/{bin,shims} 
        $path
    )
    

  3. Open a new terminal window (so the changes above take effect) and do the following:

    % brew install pyenv
    % pyenv install $PYENV_VERSION # Install your preferred Python.
    % pyenv global $PYENV_VERSION  # Make it your default Python.
    % pip install -U pip           # Update pip.
    % pip install -U --user pipx   # Install pipx into ~/.local/bin
    % pipx install pipenv          # pipx is like brew but for Python.
    

  4. Add the following to your .zshrc file:

    eval "$( pyenv init - )"
    eval "$( pip completion --zsh )"
    eval "$( register-python-argcomplete pipx )"
    eval "$( pipenv --completion )"
    

  5. Restart your shell (by typing exec zsh or opening a new terminal window) for the above to take effect.

这篇关于如何在 Mac 上使用 pipenv?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆