gpt4 book ai didi

Pyenv 无法切换 Python 版本

转载 作者:行者123 更新时间:2023-12-05 06:23:21 33 4
gpt4 key购买 nike

我已经安装了 pyenv,但是它没有执行最基本的功能,即切换 Python 版本。以下终端命令演示了这一点。

the file `main.py` is equivalent to:
import sys
print (sys.version)

Admins-MacBook-Pro-4:kylefoley kylefoley$ pyenv versions
system
* 2.7.14 (set by PYENV_VERSION environment variable)
3.5.3
3.6.1
3.7.3
pypy3.6-7.1.1
Admins-MacBook-Pro-4:kylefoley kylefoley$ pyenv global 3.5.3
Admins-MacBook-Pro-4:kylefoley kylefoley$ pyenv exec python main.py
2.7.14 (default, Oct 17 2019, 00:01:43)

如您所见,当我运行 main.py 时,出现的版本是 2.7。很多人都有这个问题。一种常见的解决方案是将

eval "$(pyenv init -)"

在我已经完成的 bash_profile 上没有帮助。在这里 Cannot switch Python with pyenv建议:

Put the PATH and shell environment vars into your .bash_profile (or whatever file your distro uses).

但是他说的是什么 PATH 和什么 shell 环境变量?

我的 .bashrc 文件也是这样的:

export PATH="/Users/kylefoley/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

如有任何帮助,我们将不胜感激。另一件事,当我运行以下命令时,我得到以下输出:

Admins-MacBook-Pro-4:kylefoley kylefoley$ python
Python 3.6.1rc1 (default, Mar 4 2017, 22:58:58)

最佳答案

问题是 .bashrc 不是 sourcenon-login 模式。

Bash 的初始化文件:

  • 登录方式:
    • /etc/profile
    • ~/.bash_profile, ~/.bash_login, ~/.profile(只有第一个存在)
  • 交互式非登录:
    • /etc/bash.bashrc(一些 Linux;不在 Mac OS X 上)
    • ~/.bashrc
  • 非交互式:
    • 源文件在$BASH_ENV

在 macOS 上,终端应用打开的默认 Bash shell 是交互式登录 shell,但在 Linux 上,终端应用打开的默认 shell 是交互式非登录 shell。

解决方案

奇怪的交互式、非登录加载要求在其他情况下也会让人们感到困惑。 最佳解决方案 是将 ~/.bashrc 的加载要求更改为仅交互,这正是大多数 Linux 发行版正在做的。

# write content below into ~/.bash_profile

# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi

这应该是您想要的解决方案。我建议每个 Bash 用户在配置文件中设置它。

引用资料

关于Pyenv 无法切换 Python 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58428548/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com