无法使用pyenv激活virtualenv

时间:2017-08-08 20:05:56

标签: python bash virtualenv pyenv

我跑:

pyenv activate new_app

我得到了:

Failed to activate virtualenv.

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

我正在尝试按照本教程:https://tutorials.technology/tutorials/59-Start-a-flask-project-from-zero-building-api-rest.html

其他信息:

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)

我最近将.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

如何正确启动virtualenv?

2 个答案:

答案 0 :(得分:23)

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

应该在.bashrc,而不是.bash_profile。后者仅由 login shell执行,前者由所有交互式shell执行。

答案 1 :(得分:6)

  1. 将以下几行添加到您的〜/ .bash_profile
for (int i = 0; i < dt_ReportList.Items.Count; i++)
{
   if (!(dt_ReportList.Items[i] is DataRowView dataRowView))
      continue;

   for (int j = 0; j < dt_ReportList.Columns.Count; j++)
   {
      xcelApp[i + 2, j + 1] = dataRowView.Row[j];
   }
}
  1. 重新启动shell或运行命令:
eval "$(pyenv init -)"  
eval "$(pyenv virtualenv-init -)"

注意:

如果您使用的是 zsh shell macOS Catalina 和/或 Big Sur 的默认设置),则必须使用source ~/.bash_profile 文件而不是~/.zprofile