返回介绍

0.3.4 Python

发布于 2025-05-06 21:45:52 字数 3825 浏览 0 评论 0 收藏

本书主要关于 CMake,但是其中的一些方法,需要使用 Python。因此,也需要对 Python 进行安装:解释器、头文件和库。Python 2.7 的生命周期结束于 2020 年,因此我们将使用 Python 3.5。

在 Ubuntu 14.04 LTS 上(这是 Travis CI 使用的环境,我们后面会讨论),Python 3.5 可以安装如下:

sudo apt-get install python3.5-dev

Windows 可使用 MSYS2 环境,Python 安装方法如下(假设是 64 位版本):

$ pacman -S mingw64/mingw-w64-x86_64-python3
$ pacman -S mingw64/mingw-w64-x86_64-python3-pip
$ python3 -m pip install pipenv

为了运行已经写好的测试机制,还需要一些特定的 Python 模块。可以使用包管理器在系统范围内安装这些包,也可以在隔离的环境中安装。建议采用后一种方法:

  • 可以在不影响系统环境的情况下,将安装包进行清理/安装。
  • 可以在没有管理员权限的情况下安装包。
  • 可以降低软件版本和依赖项冲突的风险。
  • 为了复现性,可以更好地控制包的依赖性。

为此,我们准备了一个 Pipfile 。结合 pipfile.lock ,可以使用 Pipenv ( http://pipenv.readthedocs )。创建一个独立的环境,并安装所有包。要为示例库创建此环境,可在库的顶层目录中运行以下命令:

$ pip install --user pip pipenv --upgrade
$ pipenv install --python python3.5

执行 pipenv shell 命令会进入一个命令行环境,其中包含特定版本的 Python 和可用的包。执行 exit 将退出当前环境。当然,还可以使用 pipenv run 在隔离的环境中直接执行命令。

或者,可以将库中的 requirements.txt 文件与 Virtualenv ( http://docs.pythonguide.org/en/latest/dev/virtualenvs/ ) 和 pip 结合使用,以达到相同的效果:

$ virtualenv --python=python3.5 venv
$ source venv/bin/activate
$ pip install -r requirements.txt

可以使用 deactivate 命令退出虚拟环境。

另一种选择是使用 Conda 环境,我们建议安装 Miniconda 。将把最新的 Miniconda 安装到 GNU/Linux 的 $HOME/Deps/conda 目录(从 https://repo.continuum.io/miniconda/miniconda3-latestlinux-x86_64.sh 下载) 或 macOS(从 https://repo.continuum.io/miniconda/miniconda3-latestmacosx-x86_64.sh 下载):

$ curl -Ls https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh > miniconda.sh
$ bash miniconda.sh -b -p "$HOME"/Deps/conda &> /dev/null
$ touch "$HOME"/Deps/conda/conda-meta/pinned
$ export PATH=$HOME/Deps/conda/bin${PATH:+:$PATH}
$ conda config --set show_channel_urls True
$ conda config --set changeps1 no
$ conda update --all
$ conda clean -tipy

Windows 上,可以从 https://repo.continuum.io/miniconda/Miniconda3-latest-Windows-x86_64.exe 下载最新的 Miniconda 。该软件包可以使用 PowerShell 安装,如下:

$basedir = $pwd.Path + "\"
$filepath = $basedir + "Miniconda3-latest-Windows-x86_64.exe"
$Anaconda_loc = "C:\Deps\conda"
$args = "/InstallationType=JustMe /AddToPath=0 /RegisterPython=0 /S /D=$Anaconda_loc"
Start-Process -FilePath $filepath -ArgumentList $args -Wait -Passthru
$conda_path = $Anaconda_loc + "\Scripts\conda.exe"
$args = "config --set show_channel_urls True"
Start-Process -FilePath "$conda_path" -ArgumentList $args -Wait -Passthru
$args = "config --set changeps1 no"
Start-Process -FilePath "$conda_path" -ArgumentList $args -Wait -Passthru
$args = "update --all"
Start-Process -FilePath "$conda_path" -ArgumentList $args -Wait -Passthru
$args = "clean -tipy"
Start-Process -FilePath "$conda_path" -ArgumentList $args -Wait -Passthru

安装了 Conda 后, Python 模块可以按如下方式安装:

$ conda create -n cmake-cookbook python=3.5
$ conda activate cmake-cookbook
$ conda install --file requirements.txt

执行 conda deactivate 将退出 conda 的环境。

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。