mac 多版本python管理及基本使用

Mac上自带了Python2.x的版本,有时须要使用Python3.0以上版本作开发,若是担忧删除自带python版本出现问题或者有系统依赖2.x版本,能够安装多个版本的Python。html

Mac系统自带的python路径:/System/Library/Frameworks/Python.framework/Versions/x.xpython

本身安装的python路径:/Library/Frameworks/Python.framework/Versions/x.xlinux

 

若是以前本身安装过python能够按照如下步骤删除:git

一、删除Python框架github

sudo rm -rf /Library/Frameworks/Python.framework/Versions/x.xshell

二、删除Python程序数据库

sudo rm -rf “/Applications/Python x.x”npm


  • 首先使用pyenv管理/切换python版本

Pyenv只会管理经过Pyenv安装的Python版本,你本身在Python官网上下载的直接安装的Pyenv并不能被管理!!!一样除了系统自带的python包外,其余直接安装的python包是识别不出来的,即便使用的brew安装的也识别不出来。编程

  1. Homebrew macOS 缺失的软件包管理器(https://brew.sh/index_zh-cn.html

在终端执行:windows

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"


Homebrew 经常使用命令一览

$ brew --help #简洁命令帮助

$ man brew #完整命令帮助

$ brew install git #安装软件包(这里是示例安装的Git版本控制)

$ brew uninstall git #卸载软件包

$ brew search git #搜索软件包

$ brew list #显示已经安装的全部软件包

$ brew update #同步远程最新更新状况,对本机已经安装并有更新的软件用*标明

$ brew outdated #查看已安装的哪些软件包须要更新

$ brew upgrade git #更新单个软件包

$ brew info git #查看软件包信息

$ brew home git #访问软件包官方站

$ brew cleanup #清理全部已安装软件包的历史老版本

$ brew cleanup git #清理单个已安装软件包的历史版本

参考:https://www.jianshu.com/p/28cbe523d08e


  1. 安装pyenv

具体操做可见(https://github.com/pyenv/pyenv#homebrew-on-mac-os-x

推荐帖子(https://www.jianshu.com/p/2b0b652eaa50

若是想直接操做见以下:

brew update brew install pyenv brew upgrade pyenv #以后若是须要更新pyenv

而后在 .bash_profile 文件中添加 

eval “$(pyenv init -)” 

在安装成功以后须要在.bashrc或者.bash_profile中添加三行来开启自动补全

export PATH="$HOME/.pyenv/bin:$PATH" eval "$(pyenv init -)" eval "$(pyenv virtualenv-init -)"

详细的pyenv的命令参考https://github.com/yyuu/pyenv/blob/master/COMMANDS.md

安装后的路径:/用户/dong/.pyenv/versions

  1. 安装python

查看已安装Python版本

pyenv versions

带*号的是当前路径下所使用的Python版本(此时只有system)

查看可以安装的版本:

pyenv install --list 

安装须要的版本: 

pyenv install 3.4.3 -v

完成后更新数据库

pyenv rehash

查看系统已安装的版本: 

pyenv versions

查看pyenv的命令

pyenv -h

  1. 切换python版本 

pyenv global x.x.x

pyenv versions

(注:pyenv version之列出当前的版本,pyenv versions列出全部版本以及当前选中的版本)


Python版本的设置:

  • pyenv global <version> # 全局设置python版本为指定版本,设置全局的 Python 版本,经过将版本号写入 ~/.pyenv/version 文件的方式。
  • pyenv local <version> # 设置当前路径下python版本为指定版本,设置 Python 本地版本,经过将版本号写入当前目录下的 .python-version 文件的方式。经过这种方式设置的 Python 版本优先级较 global 高。
  • pyenv shell <version> # 设置当前shell窗口使用的python版本为指定版本,设置面向 shell 的 Python 版本,经过设置当前 shell 的 PYENV_VERSION 环境变量的方式。这个版本的优先级比 local 和 global 都要高。–unset 参数能够用于取消当前 shell 设定的版本。

优先级:shell > local > global

pyenv会从当前目录开始向上逐级查找.python-versiob文件,直到根目录为止,若找不到,则使用global版本。


  1. Python卸载

pyenv isntall <version> # 安装版本号为<version>的Python pyenv uninstall <version> #卸载版本号为<version>的Python

  1. 说明:

系统自带的脚本会以/usr/bin/python的方式直接调用老版本的python,于是不会对系统脚本产生影响; 

使用pip安装第三方模块时会安装到~/.pyenv/versions/3.4.1下,不会和系统模块发生冲突。 

使用pip安装模块后,可能须要执行pyenv rehash更新数据库 

  1. pip 是通用的Python包管理工具。提供了对 Python 包的查找、下载、安装、卸载的功能。

若是安装的Python 2 >=2.7.9 或者Python 3 >=3.4 那么Python自带了pip,因此不用安装,配置下它的环境就能够

路径:Python安装路径\Scripts

1)pip的自我更新

$ pip install -U pip

2)安装 PyPI软件包 

$ pip install SomePackage # latest version

$ pip install SomePackage==1.0.4 # specific version

$ pip install 'SomePackage>=1.0.4' # minimum version

3)卸载安装包

$ pip uninstall SomePackage

4)查看列出已安装的软件包

$ pip list

5)查找须要更新的软件包

$ pip list --outdated

6)更新软件包

$ pip install --upgrade SomePackage

7)查看软件包的详细信息

$ pip show sphinx

8)搜素软件包

$ pip search "query"

 

  1. Jupyter Notebook

Jupyter Notebook(此前被称为 IPython notebook)是一个交互式笔记本,支持运行 40 多种编程语言。

网站(https://jupyter.readthedocs.io/en/latest/install.html

1)Installing Jupyter with pip

若是是python3:

python3 -m pip install --upgrade pip

python3 -m pip install jupyter

果是python2:

python -m pip install --upgrade pip

python -m pip install jupyter

2)启动

jupyter notebook


注:Jupyter Notebook导出pdf错误

https://nbconvert.readthedocs.io/en/latest/install.html#installing-tex.

pip install nbconvert# ORconda install nbconvert


  1. Conda介绍

conda相似于npm或maven的包管理工具,功能上相似 pip 和 vitualenv 的组合,分为minconda和anaconda,minconda是简化版本,只包含conda和其依赖。anaconda支持windows、mac和linux系统,且有两个类型的版本,分别是GUI和command line版本,前者是图形界面,后者是命令行界面,占用资源较少。conda有python3.x和python2.x系列两个版本。

conda 的环境管理与 virtualenv 是基本上是相似的操做。

如下内容来源于https://foofish.net/anaconda-install.html


# 查看帮助

conda -h

# 基于python3.6版本建立一个名字为python36的环境

conda create --name python36 python=3.6

# 激活此环境

activate python36 source activate python36

# linux/mac# 再来检查python版本,显示是 3.6

python -V

# 退出当前环境

deactivate python36

# 删除该环境

onda remove -n python36 --all

# 或者

conda env remove -n python36

# 查看因此安装的环境conda info -ebase * /Applications/anaconda3

python36 /Applications/anaconda3/envs/python36

 

conda 的包管理功能可 pip 是同样的,固然你选择 pip 来安装包也是没问题的。

# 安装 matplotlib

conda install matplotli

b# 查看已安装的包

conda list

# 包更新

conda update matplotlib

# 删除包

conda remove matplotlib

在 conda 中 anything is a package。conda 自己能够看做是一个包,python 环境能够看做是一个包,anaconda 也能够看做是一个包,所以除了普通的第三方包支持更新以外,这3个包也支持。好比:

# 更新conda自己

conda update conda

# 更新anaconda 应用

conda update anaconda

# 更新python,假设当前python环境是3.6.1,而最新版本是3.6.2,那么就会升级到3.6.2

conda update python

修改镜像地址Anaconda 的镜像地址默认在国外,用 conda 安装包的时候会很慢,目前可用的国内镜像源地址有清华大学的。修改 ~/.condarc (Linux/Mac) 或 C:\Users\当前用户名\.condarc (Windows) 配置:

channels: - https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ - defaultsshow_channel_urls: true

若是使用conda安装包的时候仍是很慢,那么能够考虑使用pip来安装,若是使用conda安装包的时候仍是很慢,那么能够考虑使用pip来安装,\pip\pip.ini (Windows) 配置:

[global]trusted-host = pypi.douban.comindex-url = http://pypi.douban.com/simple

  1. conda的理解

下面是粘贴官网的介绍,查了不少感受没有比这个更贴切的解释了,原文粘贴以下(https://conda.io/docs/index.html

Package, dependency and environment management for any language—Python, R, Ruby, Lua, Scala, Java, JavaScript, C/ C++, FORTRAN

Conda is an open source package management system and environment management system that runs on Windows, macOS and Linux. Conda quickly installs, runs and updates packages and their dependencies. Conda easily creates, saves, loads and switches between environments on your local computer. It was created for Python programs, but it can package and distribute software for any language.

Conda as a package manager helps you find and install packages. If you need a package that requires a different version of Python, you do not need to switch to a different environment manager, because conda is also an environment manager. With just a few commands, you can set up a totally separate environment to run that different version of Python, while continuing to run your usual version of Python in your normal environment.

In its default configuration, conda can install and manage the thousand packages at repo.continuum.io that are built, reviewed and maintained by Anaconda®.

Conda can be combined with continuous integration systems such as Travis CI and AppVeyor to provide frequent, automated testing of your code.

The conda package and environment manager is included in all versions of Anaconda®, Minicondaand Anaconda Repository. Conda is also included in Anaconda Enterprise , which provides on-site enterprise package and environment management for Python, R, Node.js, Java and other application stacks. Conda is also available on PyPI, although that approach may not be as up to date.

其中还推荐一篇文以及翻译文,可以更好的理解:

原文: https://jakevdp.github.io/blog/2016/08/25/conda-myths-and-misconceptions/

翻译: https://blog.csdn.net/taopanpantao/article/details/53982752

  1. 后续更新。。。