简体   繁体   English

无法使用 pyenv 激活 virtualenv

[英]Failed to activate virtualenv with pyenv

I run:我跑:

pyenv activate new_app

And I get:我得到:

Failed to activate virtualenv.

Perhaps pyenv-virtualenv has not been loaded into your shell properly.
Please restart current shell and try again.

I am trying to follow this tutorial: https://tutorials.technology/tutorials/59-Start-a-flask-project-from-zero-building-api-rest.html我正在尝试遵循本教程: https://tutorials.technology/tutorials/59-Start-a-flask-project-from-zero-building-api-rest.html

Other info:其他信息:

bash-3.2$ python --version
Python 3.6.0

bash-3.2$ pyenv version
3.6.0 (set by /Users/me/Projects/flask_api/.python-version)

bash-3.2$ pwd
/Users/me/Projects/flask_api

bash-3.2$ pyenv versions
  system
  3.5.1
  3.5.1/envs/my_env_3_5_1
* 3.6.0 (set by /Users/me/Projects/flask_api/.python-version)
  3.6.0/envs/new_app
  flask_app
  my_env_3_5_1
  new_app

bash-3.2$ virtualenv --version
15.1.0

bash-3.2$ pyenv virtualenvs
3.5.1/envs/my_env_3_5_1 (created from /Users/me/.pyenv/versions/3.5.1)
  3.6.0/envs/new_app (created from /Users/me/.pyenv/versions/3.6.0)
  flask_app (created from /System/Library/Frameworks/Python.framework/Versions/2.7)
  my_env_3_5_1 (created from /Users/me/.pyenv/versions/3.5.1)
  new_app (created from /Users/me/.pyenv/versions/3.6.0)

I recently made my .bash_profile it contains:我最近制作了我的.bash_profile它包含:

bash-3.2$ cat ~/.bash_profile 
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
exec "$SHELL"
if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)"; fi

What should I do to properly start virtualenv?我应该怎么做才能正确启动 virtualenv?

That

eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

should be in .bashrc , not .bash_profile .应该在.bashrc ,而不是.bash_profile The latter is executed only by login shells, the former by all interactive shells.后者仅由登录shell 执行,前者由所有交互式 shell 执行。

  1. Add the lines below to your ~/.bash_profile or ~/.zprofile ~/.zprofile添加到您的~/.bash_profile~/.zprofile
eval "$(pyenv init -)"  
eval "$(pyenv virtualenv-init -)"
  1. Restart shell or run the command:重新启动 shell 或运行命令:
source ~/.bash_profile

Note:注意:

If you are using zsh shell (default for macOS Catalina and/or Big Sur ) you have to use ~/.zprofile file rather than ~/.bash_profile如果您使用zsh shellmacOS Catalina和/或Big Sur 的默认设置),则必须使用~/.zprofile文件而不是~/.bash_profile

For me the below fixed my problem.对我来说,以下解决了我的问题。 I am using MacBook Pro thanks @Kalanos我正在使用 MacBook Pro 感谢@Kalanos

eval "$(pyenv init --path)"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Within ~/.zshrc~/.zshrc

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Sadly the init lines alone (mentioned in the pyenv and pyenv-virtualenv instructions) did not work for me遗憾的是,单独的 init 行(在 pyenv 和 pyenv-virtualenv 说明中提到)对我不起作用

For those who still struggled to get python --version prints out the expected results, after spending few hours like me, here's one solution which works (on MacOS environment, if that matters).对于那些仍在努力获得python --version打印出预期结果的人来说,在像我一样花了几个小时之后,这里有一个可行的解决方案(在 MacOS 环境中,如果这很重要的话)。

Add these line to your ~/.zshrc or ~/.bash_profile or whatever:将这些行添加到您的~/.zshrc~/.bash_profile或其他任何内容:

eval "$(pyenv init --path)"  # <-- super important!
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

For context, see https://github.com/pyenv/pyenv/issues/849#issuecomment-875875617 .有关上下文,请参阅https://github.com/pyenv/pyenv/issues/849#issuecomment-875875617

For sake of performance I recommend to use conditional initialization not only for this case but for all eval statements in your shell configs [~/.zshrc etc.]为了性能,我建议不仅对这种情况使用条件初始化,而且对你的 shell 配置中的所有 eval 语句都使用条件初始化 [~/.zshrc 等]

if command -v pyenv 1>/dev/null 2>&1; then
    eval "$(pyenv init --path)"
fi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM