Python 使用完整路径时pyenv virtualenv激活不起作用

Python 使用完整路径时pyenv virtualenv激活不起作用,python,virtualenv,pyenv,Python,Virtualenv,Pyenv,我在centos上安装了pyenv,并检查它是否正常工作 # curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash % Total % Received % Xferd Average Speed Time Time Time Current Dload U

我在centos上安装了
pyenv
,并检查它是否正常工作

# curl -L https://raw.githubusercontent.com/pyenv/pyenv-installer/master/bin/pyenv-installer | bash
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
104  2099  104  2099    0     0  14121      0 --:--:-- --:--:-- --:--:-- 18575
Initialized empty Git repository in /root/.pyenv/.git/
remote: Counting objects: 4531, done.
remote: Compressing objects: 100% (1831/1831), done.
remote: Total 4531 (delta 3252), reused 3506 (delta 2419), pack-reused 0
Receiving objects: 100% (4531/4531), 1019.80 KiB, done.
Resolving deltas: 100% (3252/3252), done.
Initialized empty Git repository in /root/.pyenv/plugins/pyenv-doctor/.git/
remote: Counting objects: 18, done.
remote: Compressing objects: 100% (11/11), done.
remote: Total 18 (delta 5), reused 14 (delta 4), pack-reused 0
Unpacking objects: 100% (18/18), done.
Initialized empty Git repository in /root/.pyenv/plugins/pyenv-installer/.git/
remote: Counting objects: 22, done.
remote: Compressing objects: 100% (19/19), done.
remote: Total 22 (delta 4), reused 13 (delta 0), pack-reused 0
Unpacking objects: 100% (22/22), done.
Initialized empty Git repository in /root/.pyenv/plugins/pyenv-update/.git/
remote: Counting objects: 7, done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 7 (delta 1), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (7/7), done.
Initialized empty Git repository in /root/.pyenv/plugins/pyenv-virtualenv/.git/
remote: Counting objects: 478, done.
remote: Compressing objects: 100% (309/309), done.
remote: Total 478 (delta 313), reused 271 (delta 144), pack-reused 0
Receiving objects: 100% (478/478), 259.55 KiB, done.
Resolving deltas: 100% (313/313), done.
Initialized empty Git repository in /root/.pyenv/plugins/pyenv-which-ext/.git/
remote: Counting objects: 20, done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 20 (delta 2), reused 14 (delta 1), pack-reused 0
Unpacking objects: 100% (20/20), done.

WARNING: seems you still have not added 'pyenv' to the load path.

# Load pyenv automatically by adding
# the following to ~/.bash_profile:

export PATH="/root/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
[root@localhost ~]# export PATH="/root/.pyenv/bin:$PATH"
[root@localhost ~]# eval "$(pyenv init -)"
[root@localhost ~]# eval "$(pyenv virtualenv-init -)"
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# pyenv
pyenv 1.2.1
Usage: pyenv <command> [<args>]

Some useful pyenv commands are:
   commands    List all available pyenv commands
   local       Set or show the local application-specific Python version
   global      Set or show the global Python version
   shell       Set or show the shell-specific Python version
   install     Install a Python version using python-build
   uninstall   Uninstall a specific Python version
   rehash      Rehash pyenv shims (run this after installing executables)
   version     Show the current Python version and its origin
   versions    List all Python versions available to pyenv
   which       Display the full path to an executable
   whence      List all Python versions that contain the given executable

See `pyenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/pyenv/pyenv#readme
创建virtualenv

# pyenv virtualenv 2.7.13 myenv
Collecting virtualenv
  Using cached virtualenv-15.1.0-py2.py3-none-any.whl
Installing collected packages: virtualenv
Successfully installed virtualenv-15.1.0
New python executable in /root/.pyenv/versions/2.7.13/envs/myenv/bin/python2.7
Also creating executable in /root/.pyenv/versions/2.7.13/envs/myenv/bin/python
Installing setuptools, pip, wheel...done.
Requirement already satisfied: setuptools in /root/.pyenv/versions/2.7.13/envs/myenv/lib/python2.7/site-packages
Requirement already satisfied: pip in /root/.pyenv/versions/2.7.13/envs/myenv/lib/python2.7/site-packages
当激活myenv时,其工作正常

# pyenv activate myenv
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
(myenv) [root@localhost ~]# source deactivate
pyenv-virtualenv: deactivate 2.7.13/envs/myenv
当我使用完整路径时,它会给出错误

[root@localhost ~]# which pyenv
/root/.pyenv/bin/pyenv
[root@localhost ~]# /root/.pyenv/bin/pyenv activate myenv

Failed to activate virtualenv.

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

我可以使用
pyenv
进行测试,但是当我必须在
ansible
中使用
pyenv
时,我必须给出完整路径,作为
/root/.pyenv/bin/pyenv
使用pyenv时,您需要引用想要运行的版本。试着这样做:

/root/.pyenv/versions/myenv/bin/python
这是假设您的
PYENV\u ROOT
环境变量设置为
/ROOT/.PYENV/

当您以这种方式执行python时,就像激活virtualenv一样,也会获取您在virtualenv中安装的所有库

开始一个全新的环境:

host:~ user$ pyenv virtualenv 2.7.12 myenv
Collecting virtualenv
  Downloading virtualenv-15.1.0-py2.py3-none-any.whl (1.8MB)
    100% |████████████████████████████████| 1.8MB 744kB/s
Installing collected packages: virtualenv
Successfully installed virtualenv-15.1.0
You are using pip version 8.1.1, however version 9.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
New python executable in /Users/user/.pyenv/versions/2.7.12/envs/myenv/bin/python2.7
Also creating executable in /Users/user/.pyenv/versions/2.7.12/envs/myenv/bin/python
Installing setuptools, pip, wheel...done.
Ignoring indexes: https://pypi.python.org/simple
Requirement already satisfied (use --upgrade to upgrade): setuptools in /Users/user/.pyenv/versions/2.7.12/envs/myenv/lib/python2.7/site-packages
Requirement already satisfied (use --upgrade to upgrade): pip in /Users/user/.pyenv/versions/2.7.12/envs/myenv/lib/python2.7/site-packages
激活虚拟环境并显示默认情况下未安装请求。安装它

host:~$ pyenv activate myenv
pyenv-virtualenv: prompt changing will be removed from future release. configure `export PYENV_VIRTUALENV_DISABLE_PROMPT=1' to simulate the behavior.
(myenv) host:~ $ python
Python 2.7.12 (default, Dec 15 2016, 12:49:19)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named requests
>>>
(myenv) host:~$ pip install requests
Collecting requests
  Downloading requests-2.18.4-py2.py3-none-any.whl (88kB)
    100% |████████████████████████████████| 92kB 2.0MB/s
Collecting chardet<3.1.0,>=3.0.2 (from requests)
  Downloading chardet-3.0.4-py2.py3-none-any.whl (133kB)
    100% |████████████████████████████████| 143kB 4.0MB/s
Collecting certifi>=2017.4.17 (from requests)
  Downloading certifi-2017.11.5-py2.py3-none-any.whl (330kB)
    100% |████████████████████████████████| 337kB 3.0MB/s
Collecting urllib3<1.23,>=1.21.1 (from requests)
  Downloading urllib3-1.22-py2.py3-none-any.whl (132kB)
    100% |████████████████████████████████| 133kB 5.8MB/s
Collecting idna<2.7,>=2.5 (from requests)
  Downloading idna-2.6-py2.py3-none-any.whl (56kB)
    100% |████████████████████████████████| 61kB 7.0MB/s
Installing collected packages: chardet, certifi, urllib3, idna, requests
Successfully installed certifi-2017.11.5 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22
停用并显示默认python是不同的版本:

(myenv) host:~$ pyenv deactivate
host:~$ python
Python 3.5.2 (default, Sep 21 2016, 15:26:09)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
直接运行
myenv
virtualenv

host:~$ ~/.pyenv/versions/myenv/bin/python
Python 2.7.12 (default, Dec 15 2016, 12:49:19)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>>

你能告诉我怎么用这个吗?我尝试了
/root/.pyenv/versions/myenv/bin/python activate myenv
,但失败了。您实际上没有使用该命令激活环境。这将在该环境的范围内运行python命令。包括您在其中安装的所有软件包。所以
/root/.pyenv/versions/myenv/bin/python
实际上将从您那里运行python
myenv
virtualenv。我得到的唯一问题是,使用ansible
命令
模块,我无法使用
pyenv
,必须使用
/root/.pyenv/bin/pyenv
,这不适用于activate。我想我不明白您在ansible中想要实现什么。也许你可以用ansible中的内容来更新你的OP。很抱歉造成混淆,但是想知道,如果
pyenv
指向
/root/.pyenv/bin/pyenv
,那么
pyenv activate myenv
工作正常,但是当使用完整路径作为
/root/.pyenv/bin/pyenv activate myenv
不工作时,会有什么变化?
(myenv) host:~$ pyenv deactivate
host:~$ python
Python 3.5.2 (default, Sep 21 2016, 15:26:09)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
host:~$ ~/.pyenv/versions/myenv/bin/python
Python 2.7.12 (default, Dec 15 2016, 12:49:19)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>>