且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-07-31 16:06:04

使用 pyenv 时,您需要引用您要运行的版本.尝试这样的事情:

When using pyenv you need to reference the version that you are wanting to run. Try something like this:

/root/.pyenv/versions/myenv/bin/python

假设您的 PYENV_ROOT 环境变量设置为 /root/.pyenv/

That is assuming your PYENV_ROOT environment variable is set to /root/.pyenv/

当你以这种方式执行 python 时,就像激活 virtualenv 一样,抓取你在 virtualenv 中安装的任何库.

When you execute python this way it is like activating the virtualenv, grabbing any installed libraries you had in the virtualenv as well.

开始一个全新的环境:

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

激活虚拟环境,显示默认没有安装requests.安装它.

Activate the virtual environment and show that requests is not installed by default. Install it.

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

显示它现在已安装在虚拟环境中:

Show that it is now installed in the virtual env:

(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
>>>

停用并显示默认python是不同的版本:

Deactivate and show that the default python is a different version:

(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

Run the myenv virtualenv directly

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
>>>