如何解决pyenv - 环境“已激活”,但未找到 python 和 pip
我想我的 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 init 脚本,因为在 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
init 脚本从 .bash_profile
移动到 .bashrc
。
# Put it in .bashrc
# PyEnv
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
延伸阅读
您的 bash shell 不是登录 shell。 .bash_profile
根本没有来源,跳过 pyenv init -
。
Bash 初始化
-
登录模式:
/etc/profile
-
~/.bash_profile
、~/.bash_login
、~/.profile
(仅存在的第一个)
-
交互式非登录:
-
/etc/bash.bashrc
(某些 Linux;不适用于 Mac OS X) ~/.bashrc
-
-
非交互式:
-
$BASH_ENV
中的源文件
-
Linux 上的默认 shell 是非登录的交互式 shell。 macOS 上的默认 shell 是登录的交互式 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 -)"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。