【问题标题】:pyenv - environment "activated", but python and pip not foundpyenv - 环境“激活”,但没有找到 python 和 pip
【发布时间】:2021-10-21 17:58:09
【问题描述】:

我认为我的 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

【问题讨论】:

  • type python 打印什么?
  • @RamanSailopal .profile 仅在.bash_profile.bash_login 都不存在时使用。
  • @KamilCuk 我添加了type pythonwhich python 的输出
  • 安装新的python版本后你运行pyenv rehash了吗?
  • 谢谢,我试过了还是没有效果:-(

标签: bash pyenv


【解决方案1】:

在@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 -)"

【讨论】:

    【解决方案2】:

    解决方案

    仔细阅读 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 -)"
    

    延伸阅读

    您的 bash shell 不是登录 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 中的源文件

    Linux 上的默认 shell 是非登录的交互式 shell。 macOS 上的默认 shell 是登录的交互式 shell。

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

    参考

    【讨论】:

    • 虽然你的回答有点粗鲁,但我很感激。您帮助我找到了可行的解决方案,谢谢。
    猜你喜欢
    • 2019-12-12
    • 2020-05-29
    • 2019-11-02
    • 1970-01-01
    • 2014-03-06
    • 2020-05-22
    • 2016-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多