Installation and use of Pyenv under Mac

Original address: http://www.jianshu.com/p/cea9259d87df Click to open the link

Installation and use of Pyenv under Mac


what pyenv does

pyenv is used to manage/switch python versions more conveniently
. It is very simple to install through homebrew on github. It is mainly because of unfamiliarity with relevant knowledge points that cause many problems.
The first thing you must understand is:

Pyenv will only manage the Python version installed through Pyenv, the directly installed Pyenv you downloaded from the Python official website 并不能被管理! ! ! In addition to the python package that comes with the system, 其他直接安装the python package is 识别不出来的not recognized even if it is installed by brew.

Install via homebrew:

brew install pyenv

But github prompts a sentence

After installation, you'll need to add eval "$(pyenv init -)" to your profile (as stated in the caveats displayed by Homebrew — to display them again, usebrew info pyenv). You only need to add that to your profile once.

Terminal prompt:

Caveats
To use Homebrew's directories rather than ~/.pyenv add to your profile:
export PYENV_ROOT=/usr/local/var/pyenv

To enable shims and autocompletion add to your profile:
if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi

Which file is this profile?

profileRefers to .bash_profile, hidden files under mac

Edit the .bash_profile file

Enter the following command in the terminal to enter the home directory of the current user

$ cd ~ 

Enter the following command to open the .bash_profile file

open .bash_profile

If it does not exist, enter the following command to create the file

touch .bash_profile

edit file

open -e .bash_profile

Added in the pop-up .bash_profile file

export PYENV_ROOT=/usr/local/var/pyenv
if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi

command + s to save the file

Enter the following command in the terminal to refresh the previously configured .bash_profile file.

source .bash_profile
Use of pyenv
  • Check installed python version
$ pyenv versions
  • Check the current python version in use
$ pyenv version
  • Install a python version like 3.5.2
$ pyenv install 3.5.2
  • Uninstall a python version like 3.5.2
$ pyenv uninstall  3.5.2
  • Set the global python version like 3.5.2
$ pyenv global 3.5.2
//很多人不推荐这么做,说是mac操作系统的文件也会调用原生的2.7的python版本
//这种说法感觉有点:恐惧来自未知的感觉.持保留意见
  • Set the directory level python version such as 3.5.2
$ pyenv local 3.5.2
  • Set python version like 3.5.2 for current shell session
$ pyenv shell 3.5.2

If restricting a specific python version in the specified file directory requires the following:

$ cd + 文件路径
$ pyenv local 3.5.2 
  • How to check the current settings version
 $ pyenv versions
 * system (set by /usr/local/var/pyenv/version)
    2.7.8
    2.7.10
  #当前设定版本前会有星号标识
  • How to cancel the current set version
$ pyenv local --unset 
  #其他两种命令同样参数

After using the above command to specify the python version, after the file is deleted, even if the file with the same name is in the original path, the python version is still the global version set by the system.
[pyenv command list][1]
[1]: https://github. com/yyuu/pyenv/blob/master/COMMANDS.md#command-reference

2. How does Pycharm switch the installation package managed by Pyenv

当你使用pyenv切换了python版本时.Pycharm不会自动切换到对应的python. 需要自己手动的添加对应版本的绝对路径到Pycharm.
如图.在Add Loacl 中添加pyenv管理的绝对路径.


add-local.png
add-local.png
2.1 . pyenv 管理的安装包路径

目前有两种情况.

  1. 直接安装的pyenv,即pyenv在默认的目录:

        pyenv管理的python版本路径:  ~/.pyenv/versions/

  2. 通过 Homebrew 安装的 pyenv:

        pyenv管理的python版本路径:  /usr/local/var/pyenv/versions
        
  3. 如果上述两种方法都找不到:
      
        直接通过 pyenv 随意安装一个版本(2.7.9).终端会有如下提示:

        Downloading Python-2.7.9.tar.xz...
        -> https://www.python.org/ftp/python/2.7.9/Python-2.7.9.tar.xz
        Installing Python-2.7.9...
        patching file ./Lib/site.py
        patching file ./Lib/ssl.py
        Installed Python-2.7.9 to /usr/local/var/pyenv/versions/2.7.9
    
        你找的路径已经出来了--> /usr/local/var/pyenv/versions/


[pyenv 官方 错误查看地址][1-c]
[1-c]:https://github.com/yyuu/pyenv/wiki/Common-build-problems

3. pyenv 管理的版本安装包区别简介

当你在终端输入:

$ pyenv install --list

你会看到如下版本列表



Available versions:
  2.1.3
  ...
  2.7.12
  3.0.1
  3.5-dev 
  anaconda-1.4.0
  ...
  anaconda2-4.1.0
  anaconda3-4.1.0
  ironpython-dev
  ironpython-2.7.5
  jython-dev
 ...
  jython-2.7.1b3
  miniconda-2.2.2
  ...
  miniconda3-4.0.5
  pypy-c-jit-latest
  ...
  pypy3.3-5.2-alpha1
  stackless-dev
  ...
  stackless-3.4.1

解释如下:
1. 纯数字的(如2.1.3, 2.7.12)是官方发布版本,
2.anaconda 也是一个python版本,只是自带了很多用于科学计算的第三方包和管理这些第三方包的管理工具(类似于pip)
3.miniconda- 是anaconda的迷你版(仅仅包涵对应的python版本和第三方包管理工具,第三方包如果使用需要自己下载)
3.IronPython 是一种在 NET 和 Mono 上实现的 Python 语言.使用.net开发
4.JPython是完全用JPython实现的python,它支持用java扩展,
5.和CPythonPyPy的区别是使用了JIT(即时编译)技术

anaconda简介

具体参看[他人简介][anaconda]
[anaconda]:http://www.jianshu.com/p/2f3be7781451
Conda的包管理(anaconda 带的管理工具)
这个工具有点特别可以下载其他版本的anaconda版本.自成一个工具(conda将conda、python等都视为package)
Conda的包管理就比较好理解了,这部分功能与pip类似。

// 安装scipy
conda install scipy
// conda会从从远程搜索scipy的相关信息和依赖项目,对于python 3.4,conda会同时安装numpy和mkl(运算加速的库)

// 查看已经安装的packages
conda list
# 最新版的conda是从site-packages文件夹中搜索已经安装的包,不依赖于pip,因此可以显示出通过各种方式安装的包

conda的一些常用操作如下

# 查看当前环境下已安装的包
conda list

// 查看某个指定环境的已安装包
conda list -n python34

// 查找package信息
conda search numpy

// 安装package
conda install -n python34 numpy
// 如果不用-n指定环境名称,则被安装在当前活跃环境
// 也可以通过-c指定通过某个channel安装

// 更新package
conda update -n python34 numpy

// 删除package
conda remove -n python34 numpy

conda将conda、python等都视为package,因此,完全可以使用conda来管理conda和python的版本,例如

// 更新conda,保持conda最新
conda update conda

// 更新anaconda
conda update anaconda

// 更新python
conda update python
// 假设当前环境是python 3.4, conda会将python升级为3.4.x系列的当前最新版本

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325713364&siteId=291194637