Qingquan-Li / blog

My Blog
https://Qingquan-Li.github.io/blog/
132 stars 16 forks source link

安装Python(Linux/macOS/Windows) #80

Open Qingquan-Li opened 6 years ago

Qingquan-Li commented 6 years ago

参考:


在 Linux 系 统 中 安 装 Python

查 看 已 安 装 的 Python 版 本

运 行 Terminal 终 端( 如 果 你 使 用 的 是 Ubuntu, 可 按 Ctrl + Alt + T):

$ python --version
Python 2.7.6
$ python3 --version
Python 3.5.0

安 装 Python 3

大 多 数 Linux 系 统 都 默 认 安 装 了 Python ,如 果 没 有 安 装 Python 3, 或 者 你 想 安 装 较 新 的 Python 3 版 本, 只 需 执 行 几 个 命 令 即 可。 我 们 使 用 一 个 名 为 deadsnakes 的 包, 它 让 安 装 多 个 Python 版 本 变 得 很 容 易:

$ sudo add-apt-repository ppa:fkrull/ deadsnakes
$ sudo apt-get update
$ sudo apt-get install python3.5

上 面 这 些 命 令 在 你 的 系 统 中 安 装 Python 3.5。 下 面 的 命 令 启 动 一 个 运 行 Python 3.5 的 终 端 会 话:

$ python3.5 
>>>


在 macOS 系 统 中 安 装 Python

查 看 已 安 装 的 Python 版 本

运 行 Terminal 终 端:

$ python --version
Python 2.7.6
$ python3 --version
Python 3.5.0

安 装 Python 3

大 多 数 macOS 系 统 都 默 认 安 装 了 Python 2 ,但 是 没 有 安 装 Python 3 。

Python pkg安装:https://www.python.org/downloads/

这 里 使 用 Homebrew 来 安 装 Python 3 , Homebrew 安装方法参见:Homebrew:macOS软件包管理工具

安 装 最 新 的 Python 3 版 本:

$ brew install python3

安装指定的版本

部分旧版本不能使用 brew 安装( brew 默认安装最新版),需要直接下载 pkg 文件进行包安装,例如安装 Python 3.5.2 需要下载对应的安装包 python-3.5.2-macosx10.6.pkg

$ brew search python3@
==> Formulae
boost-python3              python3 ✔                  python@3 ✔


在 Windows 系 统 中 安 装 Python 3

查看已安装的 Python 版本

运 行 命 令 行 工 具 :

> python --version
Python 3.5.0

安 装 Python 3

Windows 系 统 默 认 未 安 装 Python。

  1. 访 问 http://python.org/downloads/ ,并 单 击 你 要 安 装 的 Python 版 本。
  2. 下 载 安 装 程 序, 并 在 运 行 它 时 选 择 复 选 框 Add Python to PATH。 这 让 你 不 用 手 工 修 改 系 统 的 环 境 变 量, 在 执 行 命 令 python 时 也 无 需 指 定 其 完 整 路 径。
  3. 安 装 Python 后, 打 开 一 个 新 的 命 令 行 窗 口, 并 在 其 中 执 行 命 令 python --version 。如 果 没 有 报 错, 就 说 明 Python 安 装 好 了。

附:查 找 Python 解 释 器

如 果 你 在 安 装 Python 时 选 择 了 复 选 框 Add Python to PATH , 可 跳 过 这 一 步。

如 果 不 能 执 行 简 单 命 令 python ,你 就 需 要 告 诉 Windows 去 哪 里 查 找 Python 解 释 器。

要 确 定 Python 解 释 器 的 位 置, 请 打 开 C 盘, 并 在 其 中 查 找 名 称 以 Python 打 头 的 文 件 夹。 打 开 这 个 文 件 夹, 并 查 找 名 称 为 python( 全 部 小 写) 的 文 件。 右 击 这 个 文 件 并 选 择“ 属 性”, 你 将 在“ 位 置:” 右 边 看 到 它 的 路 径。 使 用 该 路 径 来 确 定 刚 安 装 的 Python 版 本:

$ C:\\ Python35\ python --version 
Python 3.5.0

附:将 Python 添 加 到 环 境 变 量Path 中

如 果 你 在 安 装 Python 时 选 择 了 复 选 框 Add Python to PATH , 可 跳 过 这 一 步。

鉴 于 以 上 “ 查 找 Python 解 释 器 ” ,如 果 每 次 启 动 Python 终 端 时 都 需 要 输 入 完 整 的 路 径, 那 就 太 讨 厌 了;有 鉴 于 此, 我 们 将 在 系 统 中 添 加 这 个 路 径, 让 你 只 需 使 用 命 令 python 即 可。

打 开 控 制 面 板 并 单 击“ 系 统 和 安 全”, 再 单 击“ 系 统”。 单 击“ 高 级 系 统 设 置”, 在 打 开 的 窗 口 中 单 击 按 钮“ 环 境 变 量”。

在“ 系 统 变 量” 部 分, 找 到 并 单 击 变 量 Path ,再 单 击 按 钮“ 编 辑”。 在 出 现 的 对 话 框 中, 单 击“ 变 量 值”, 并 使 用 右 箭 头 键 滚 到 最 右 边。 千 万 不 要 覆 盖 变 量 原 来 的 值, 如 果 你 不 小 心 这 样 做 了, 单 击“ 取 消” 按 钮, 再 重 复 前 面 的 步 骤。 在 变 量 值 的 末 尾 添 加 一 个 分 号, 再 添 加 文 件 python.exe 的 路 径:

%SystemRoot%\ system32\...\ System32\ WindowsPowerShell\ v1.0\; C:\ Python34

关 闭 命 令 行 窗 口, 再 打 开 一 个 新 的 命 令 行 窗 口。 这 将 在 命 令 行 窗 口 会 话 中 加 载 变 量 Path 的 新 值。 现 在 当 你 执 行 命 令 python --version 时, 将 看 到 刚 才 在 变 量 Path 中 设 置 的 Python 版 本。 现 在, 你 只 需 在 命 令 提 示 符 下 输 入 python 并 按 回 车, 就 可 启 动 Python 终 端 会 话 了。


参考:[美] Eric Matthes 《Python编程:从入门到实践》






附(macOS):

创建软链接

How can I create a symbolic link in Terminal?

Symbolic Links.txt

OSX / Linux
===========
┌── ln(1) link, ln -- make links
│   ┌── Create a symbolic link.
│   │                         ┌── the path to the intended symlink
│   │                         │   can use . or ~ or other relative paths
│   │                   ┌─────┴────────┐
ln -s /path/to/original /path/to/symlink
      └───────┬───────┘
              └── the path to the original file/folder
                  can use . or ~ or other relative paths

Windows is OPPOSITE!
==============================
┌── mklink -- make link
│       ┌── Create a symbolic link. /H for hard file, /J for hard directory
│       │                          ┌── the path to the original file/folder
│       │                    ┌─────┴────────┐
mklink /D D:\path\to\symlink D:\path\to\original
          └───────┬───────┘
                  └── the path to the intended symlink


来源 Python安装路径
系统默认 /System/Library/Frameworks/Python.framework/Versions/2.7
官网pkg安装 /Library/Frameworks/Python.framework/Versions/3.5
brew安装 /usr/local/Cellar/python


macOS 用 ln -s 命令创建符号链接(软连接),类似于 Windows 创建桌面快捷方式,符号链接(软连接)存放在 /usr/local/bin 目录中,注意 usr 是隐藏文件( command+shift+. 显示)

示例:如果使用 $ brew install python 安装了最新版 python(3.6.3 )后,在终端输入 $ python3.6 或者 $ python3.6.3 将报错,因为并没有创建软链接放进 /usr/local/bin 目录中;

当然,终端中输入 $ /usr/local/Cellar/python3/3.6.3/bin/python3 可以使用 python3.6.3 ,但是比较麻烦


卸载 Python

参考:https://blog.csdn.net/tymatlab/article/details/78608704

一、卸载 Homebrew 安装的 Python

brew uninstall python

二、卸载官网 pkg 安装的 Python

  1. 删除框架:/Library/Frameworks/Python.framework
  2. 删除应用目录:Applications/Python 3.5
  3. 删除指向 Python 的 link 链接:
    $ cd /usr/local/bin/
    $ ls -l /usr/local/bin | grep '/Library/Frameworks/Python.framework/Versions/3.5'  # 查看链接
    $ brew prune # 清除链接和目录