创建开发环境#
要测试代码更改,您需要从源代码构建 pandas,这需要 C/C++ 编译器和 Python 环境。如果您在进行文档更改,您可以跳到 contributing to the documentation ,但如果您跳过创建开发环境,您将无法在推送更改之前在本地构建文档。建议您也安装 pre-commit hooks 。
步骤 1:安装 C 编译器#
具体操作将取决于您的平台。如果您选择在下一步中使用 Docker 或 GitPod,则可以跳过此步骤。
Windows
您需要 Build Tools for Visual Studio 2022 。
备注
您**不**需要安装 Visual Studio 2022。您只需要 “Build Tools for Visual Studio 2022”,可以在向下滚动到 “All downloads” -> “Tools for Visual Studio” 中找到。在安装程序中,选择 “Desktop development with C++” 工作负载。
或者,您可以使用 vs_BuildTools.exe 在命令行中安装所需的组件。
或者,您可以使用 WSL 并参考下面的 Linux 说明。
macOS
要使用 mamba 基础编译器,您需要使用 xcode-select --install 安装 Developer Tools。
如果您更喜欢使用其他编译器,可以在此处找到通用信息:https://devguide.python.org/setup/#macos
Linux
对于基于 Linux 的 mamba 安装,您无需在 mamba 环境之外安装任何其他组件。下面的说明仅在您的设置不基于 mamba 环境时才需要。
一些 Linux 发行版将预装 C 编译器。要找出您系统上安装了哪些编译器(以及版本)::
# for Debian/Ubuntu:
dpkg --list | grep compiler
# for Red Hat/RHEL/CentOS/Fedora:
yum list installed | grep -i --color compiler
GCC (GNU Compiler Collection) 是一个广泛使用的编译器,支持 C 和其他多种语言。如果 GCC 在已安装的编译器列表中,则无需其他操作。
如果没有安装 C 编译器,或者您想升级,或者您使用的是不同的 Linux 发行版,请咨询您喜欢的搜索引擎以获取编译器安装/更新说明。
如果您遇到任何困难,请通过开设 issue 或在我们的贡献者社区 Slack 上联系我们。
步骤 2:创建隔离环境#
开始之前,请::
cd进入您刚刚使用 clone 命令创建的 pandas 源代码目录
选项 1:使用 mamba(推荐)#
安装 miniforge 以获取 mamba
确保您的 mamba 是最新的(
mamba update mamba)使用以下命令创建并激活
pandas-devmamba 环境:
mamba env create --file environment.yml
mamba activate pandas-dev
选项 2:使用 pip#
您需要安装至少是 pandas 所支持的 minimum Python version 。您还需要安装 setuptools 51.0.0 或更高版本才能构建 pandas。
Unix/macOS (使用 virtualenv)
# Create a virtual environment
# Use an ENV_DIR of your choice. We'll use ~/virtualenvs/pandas-dev
# Any parent directories should already exist
python3 -m venv ~/virtualenvs/pandas-dev
# Activate the virtualenv
. ~/virtualenvs/pandas-dev/bin/activate
# Install the build dependencies
python -m pip install -r requirements-dev.txt
Unix/macOS (使用 pyenv)
Consult the docs for setting up pyenv here.
# Create a virtual environment
# Use an ENV_DIR of your choice. We'll use ~/Users/<yourname>/.pyenv/versions/pandas-dev
pyenv virtualenv <version> <name-to-give-it>
# For instance:
pyenv virtualenv 3.9.10 pandas-dev
# Activate the virtualenv
pyenv activate pandas-dev
# Now install the build dependencies in the cloned pandas repo
python -m pip install -r requirements-dev.txt
Windows
以下是在 Windows 上使用 Powershell 设置虚拟环境的简要概述。有关详细信息,请参阅 official virtualenv user guide 。
请使用您选择的 ENV_DIR。我们将使用 ~\\virtualenvs\\pandas-dev,其中 ~ 是由 $env:USERPROFILE (Powershell) 或 %USERPROFILE% (cmd.exe) 环境变量指向的文件夹。任何父目录都应该已经存在。
# Create a virtual environment
python -m venv $env:USERPROFILE\virtualenvs\pandas-dev
# Activate the virtualenv. Use activate.bat for cmd.exe
~\virtualenvs\pandas-dev\Scripts\Activate.ps1
# Install the build dependencies
python -m pip install -r requirements-dev.txt
选项 3:使用 Docker#
pandas 在根目录提供了一个 DockerFile,用于构建包含完整 pandas 开发环境的 Docker 镜像。
Docker 命令
构建 Docker 镜像::
# Build the image
docker build -t pandas-dev .
运行容器::
# Run a container and bind your local repo to the container
# This command assumes you are running from your local repo
# but if not alter ${PWD} to match your local repo path
docker run -it --rm -v ${PWD}:/home/pandas pandas-dev
更简单的方法是,您可以将 Docker 与以下 IDE 集成:
Visual Studio Code
您可以使用 DockerFile 结合 .devcontainer.json 文件,启动一个与 Visual Studio Code (一款流行的免费 IDE) 的远程会话。详情请参阅 https://code.visualstudio.com/docs/remote/containers。
PyCharm (专业版)
启用 Docker 支持,并使用 Services 工具窗口构建和管理镜像,以及运行和交互容器。详情请参阅 https://www.jetbrains.com/help/pycharm/docker.html。
选项 4:使用 Gitpod#
Gitpod 是一个开源平台,它能在您的浏览器中自动创建正确的开发环境,从而减少了安装本地开发环境和处理不兼容依赖项的需要。
如果您是 Windows 用户,不熟悉使用命令行,或者这是您第一次构建 pandas,那么使用 Gitpod 构建通常会更快。以下是使用 Gitpod 构建 pandas 的详细说明: building pandas with GitPod 。
步骤 3:构建和安装 pandas#
目前有两种支持的构建 pandas 的方法:pip/meson 和 setuptools(setup.py)。过去,pandas 只支持使用 setuptools 来构建 pandas。然而,这种方法需要在 setup.py 中编写大量复杂的代码,并且由于 setuptools 的限制,在并行编译 pandas 时会遇到许多问题。
新的构建系统通过 pip (使用 PEP 517 构建) 调用 meson 后端。它会自动利用您 CPU 的所有可用核心,并且在导入 pandas 时(通过可编辑安装)自动重新构建,从而避免了手动重新构建的需要。
For these reasons, you should compile pandas with meson. Because the meson build system is newer, you may find bugs/minor issues as it matures. You can report these bugs here.
要使用 meson 编译 pandas,请运行::
# Build and install pandas
# By default, this will print verbose output
# showing the "rebuild" taking place on import (see section below for explanation)
# If you do not want to see this, omit everything after --no-build-isolation
python -m pip install -ve . --no-build-isolation --config-settings editable-verbose=true
备注
版本号是从最新的代码库标签中提取的。在构建之前,请确保从上游获取最新的标签::
# set the upstream repository, if not done already, and fetch the latest tags
git remote add upstream https://github.com/pandas-dev/pandas.git
git fetch upstream --tags
构建选项
如果您想配置安装,可以将选项从 pip 前端传递给 meson 后端。有时,您可能需要使用此功能来调整构建目录和/或切换调试/优化级别。
您可以通过在 pip 命令后附加 --config-settings builddir="your builddir here" 来将构建目录传递给 pandas。此选项允许您配置 meson 存储 C 扩展构建的位置,并支持快速重新构建。
有时候,在调试 C 扩展时,使用调试符号编译 pandas 可能会很有用。附加 --config-settings setup-args="-Ddebug=true" 即可实现。
使用 pip,可以链接多个配置设置(例如,同时指定构建目录和使用调试符号进行构建,看起来会是 --config-settings builddir="your builddir here" --config-settings=setup-args="-Dbuildtype=debug")。
使用 setup.py 编译 pandas
备注
正如 meson 后端逐渐成熟,此方法编译 pandas 将很快被弃用并移除。
要使用 setuptools 编译 pandas,请运行::
python setup.py develop
备注
如果 pandas 已安装(通过 meson),您必须先卸载它::
python -m pip uninstall pandas
这是因为 python setup.py develop 不会卸载 meson-python 用于从构建文件夹导入扩展的加载器脚本,这可能会导致 FileNotFoundError 等错误。
备注
每次 C 扩展发生更改时,您都需要重复此步骤,例如,如果您修改了 pandas/_libs 中的任何文件,或者您从 upstream/main 进行过 fetch 和 merge。
检查构建
此时,您应该能够从本地构建的版本中导入 pandas::
$ python
>>> import pandas
>>> print(pandas.__version__) # note: the exact output may differ
2.0.0.dev0+880.g2b9e661fbb.dirty
此时,您可能想尝试 running the test suite 。
保持最新构建
使用 meson 构建 pandas 时,导入 pandas 会自动触发重新构建,即使 C/Cython 文件被修改。默认情况下,此重新构建不会产生任何输出(导入只是需要更长时间)。如果您希望在导入 pandas 时看到 meson 的输出,可以设置环境变量 MESONPY_EDTIABLE_VERBOSE。例如,这将是::
# On Linux/macOS
MESONPY_EDITABLE_VERBOSE=1 python
# Windows
set MESONPY_EDITABLE_VERBOSE=1 # Only need to set this once per session
python
如果您希望每次都看到此详细输出,可以将 editable-verbose 配置设置设置为 true,如下所示::
python -m pip install -ve . --config-settings editable-verbose=true
小技巧
如果您想知道是用 setuptools 还是 meson 构建了您的 pandas,您可以检查 pandas._built_with_meson 的值,如果 meson 用于编译 pandas,则其值为 true。