pyenv - 环境 "activated",但未找到 python 和 pip

pyenv - environment "activated", but python and pip not found

我想我的 bash 初始化脚本有问题(比如 .bashrc.bash_profile)。但让我们从头开始。

我可以创建并激活 pyenv 环境,但是当我尝试使用 python 时,出现错误:-bash: python: command not found。 看起来 pyenv 了解创建和交换环境。我的意思是,它可能没有畸形。有我的尝试预览:

$ mkdir test-python-project
$ cd test-python-project/
$ pyenv versions
* system (set by /home/vagrant/.pyenv/version)
  3.7.10
  3.7.10/envs/k-pkb-env
$ pyenv virtualenv 3.7.10 test-env
Looking in links: /tmp/tmpkwojcc1e
Requirement already satisfied: setuptools in /home/vagrant/.pyenv/versions/3.7.10/envs/test-env/lib/python3.7/site-packages (47.1.0)
Requirement already satisfied: pip in /home/vagrant/.pyenv/versions/3.7.10/envs/test-env/lib/python3.7/site-packages (20.1.1)
$ pyenv activate test-env
pyenv-virtualenv: prompt changing will be removed from future release. configure export PYENV_VIRTUALENV_DISABLE_PROMPT=1 to simulate the behavior.
(test-env) $ python
-bash: python: command not found
(test-env) $ pyenv local test-env
(test-env) $ cd ..
(test-env) $ pyenv deactivate
$ cd test-python-project/
(test-env) $ python
-bash: python: command not found
(test-env) $ pip
-bash: pip: command not found
(test-env) $ pyenv version
test-env (set by /home/vagrant/Work/test-python-project/.python-version)

我不确定如何配置 bash 初始化脚本,因为在 pyenv readme 中他们建议使用 .profile,而我没有。

所以,有我的 bash 初始化:

.bashrc

$ cat .bashrc
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific environment
if ! [[ "$PATH" =~ "$HOME/.local/bin:$HOME/bin:" ]]
then
    PATH="$HOME/.local/bin:$HOME/bin:$PATH"
fi
export PATH

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=

# User specific aliases and functions

.bash_profile

$ cat .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

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

一些附加信息:

$PATH 变量

$ echo $PATH
/home/vagrant/.pyenv/plugins/pyenv-virtualenv/shims:/home/vagrant/.pyenv/bin:/home/vagrant/.local/bin:/home/vagrant/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin

这对我来说有点奇怪,因为 pyenv 添加的这个额外路径似乎不包含所需虚拟环境的路径:

$ ls /home/vagrant/.pyenv/plugins/pyenv-virtualenv/shims
activate  deactivate
$ ls /home/vagrant/.pyenv/bin
pyenv

type python

$ type python
-bash: type: python: not found

which python

$ which python
/usr/bin/which: no python in (/home/vagrant/.pyenv/plugins/pyenv-virtualenv/shims:/home/vagrant/.pyenv/bin:/home/vagrant/.local/bin:/home/vagrant/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin)

我也试了pyenv rehash,还是没有效果:

(test-env) [vagrant@centos test-python-project]$ pyenv rehash
(test-env) [vagrant@centos test-python-project]$ python
-bash: python: command not found

解决方案

仔细阅读 PYENV 指南。

您没有正确遵循 pyenv 的 README 指南。该指南告诉您将 PATH 相关操作放在 .bash_profile.profile 中。但是 eval "$(pyenv init -)".bashrc.

pyenv 初始化脚本从 .bash_profile 移动到 .bashrc

# Put it in .bashrc

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

扩展阅读

你bashshell不是登录shell。 .bash_profile 根本没有来源,跳过 pyenv init -.

Bash初始化

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

Linux 上的默认 shell 是非登录、交互式 shell。 mac上默认shellOS是登录,交互shell.

还有一个detailed explanation about the shell startup by flowblok

参考

在@Simba 的帮助下,我的配置成功了:

.bash_profile

# .bash_profile

# !!! ITS IMPORTANT THESE LINES MUST BE BEFORE . ~/.bashrc
# PyEnv - only path-related
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
# !!! ITS IMPORTANT THESE LINES ABOVE MUST BE BEFORE . ~/.bashrc



# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

.bashrc

# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific environment
if ! [[ "$PATH" =~ "$HOME/.local/bin:$HOME/bin:" ]]
then
    PATH="$HOME/.local/bin:$HOME/bin:$PATH"
fi
export PATH

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=

# User specific aliases and functions

# PyEnv - commands
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"