营造发展环境#

要测试代码更改,您将需要从源代码构建Pandas,这需要C/C++编译器和Python环境。如果您要更改文档,可以跳至 contributing to the documentation 但是,如果您跳过创建开发环境,则在推送更改之前,您将无法在本地构建文档。

使用Docker创建环境#

不需要手动设置开发环境,您可以使用 Docker 只需几个命令即可自动创建环境。Pandas提供了一种 DockerFile 在根目录下构建一个带有完整Pandas开发环境的Docker镜像。

Docker命令

构建Docker镜像::

# Build the image pandas-yourname-env
docker build --tag pandas-yourname-env .
# Or build the image by passing your GitHub username to use your own fork
docker build --build-arg gh_username=yourname --tag pandas-yourname-env .

运行容器::

# Run a container and bind your local repo to the container
docker run -it -w /home/pandas --rm -v path-to-local-pandas-repo:/home/pandas pandas-yourname-env

备注

如果您第一次绑定本地repo,则必须在以后构建C扩展。在容器内运行以下命令:

python setup.py build_ext -j 4

您需要在任何时候重新生成C扩展 pandas/_libs 改变。这最常发生在更改或合并分支时。

更简单的是,您可以将Docker与以下IDE集成:

Visual Studio代码

您可以使用DockerFile启动与Visual Studio代码(一种流行的免费IDE)的远程会话 .devcontainer.json 文件。有关详细信息,请参阅https://code.visualstudio.com/docs/remote/containers。

PyCharm(专业)

启用Docker支持,并使用服务工具窗口构建和管理映像,以及运行容器并与其交互。有关详细信息,请参阅https://www.jetbrains.com/help/pycharm/docker.html。

创建一个没有Docker的环境#

安装C编译器#

Pandas使用C扩展(主要使用Cython编写)来加速某些操作。要从源代码安装Pandas,您需要编译这些C扩展,这意味着您需要一个C编译器。此过程取决于您使用的平台。

如果您使用以下工具设置了环境 conda ,这些包裹 c-compilercxx-compiler 我将为您的平台安装一个适合您的平台的编译器,该编译器与其余的Conda包兼容。在Windows和MacOS上,您还需要安装SDK,因为它们必须单独分发。这些程序包将使用 pandas environment.yml 文件。

Windows

你将需要 Build Tools for Visual Studio 2019

警告

您不需要安装Visual Studio 2019。您只需要通过向下滚动到“所有下载”->“用于Visual Studio 2019的工具”来找到“Build Tools for Visual Studio 2019”。在安装程序中,选择“C++构建工具”工作负载。

您可以使用以下命令在命令行上安装必要的组件 vs_buildtools.exe

vs_buildtools.exe --quiet --wait --norestart --nocache ^
    --installPath C:\BuildTools ^
    --add "Microsoft.VisualStudio.Workload.VCTools;includeRecommended" ^
    --add Microsoft.VisualStudio.Component.VC.v141 ^
    --add Microsoft.VisualStudio.Component.VC.v141.x86.x64 ^
    --add Microsoft.VisualStudio.Component.Windows10SDK.17763

若要在命令行上设置正确的路径,请调用 "C:\BuildTools\VC\Auxiliary\Build\vcvars64.bat" -vcvars_ver=14.16 10.0.17763.0

macOS

要使用 conda -基于编译器,则需要使用以下命令安装开发人员工具 xcode-select --install 。否则,可在此处找到有关编译器安装的信息:https://devguide.python.org/setup/#macos

Linux

对于基于Linux的 conda 安装后,您将不需要在Conda环境之外安装任何额外的组件。只有当您的设置不是基于CONDA环境时,才需要下面的说明。

一些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) <https://gcc.gnu.org/> _是一种广泛使用的编译器,它支持C和许多其他语言。如果将GCC列为已安装的编译器,则不再需要更多内容。如果没有安装C编译器(或者您希望安装较新的版本),则可以使用以下命令安装编译器(以下示例代码中的GCC):

# for recent Debian/Ubuntu:
sudo apt install build-essential
# for Red Had/RHEL/CentOS/Fedora
yum groupinstall "Development Tools"

对于其他Linux发行版,请参考您最喜欢的搜索引擎以获取编译器安装说明。

如果您有任何困难,请通过打开一期杂志或联系 Gitter

创建一个Python环境#

现在创造一个与世隔绝的Pandas发展环境:

我们现在开始一个分三步走的过程:

  1. 安装构建依赖项

  2. 建造和安装Pandas

  3. 安装可选的依赖项

# Create and activate the build environment
conda env create -f environment.yml
conda activate pandas-dev

# or with older versions of Anaconda:
source activate pandas-dev

# Build and install pandas
python setup.py build_ext -j 4
python -m pip install -e . --no-build-isolation --no-use-pep517

此时,您应该能够从本地构建的版本导入Pandas:

$ python
>>> import pandas
>>> print(pandas.__version__)
0.22.0.dev0+29.g4ad6d4d74

这将创建新环境,并且不会影响任何现有环境,也不会影响任何现有的Python安装。

要查看您的环境:

conda info -e

要返回到根环境::

conda deactivate

查看完整的Conda文档 here

创建一个Python环境(Pip)#

如果您没有在您的开发环境中使用Conda,请遵循以下说明。您将需要至少拥有 minimum Python version 这是Pandas所支持的。你还需要有 setuptools 51.0.0或更高版本以构建Pandas。

Unix / 支持虚拟环境的MacOS

# 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

# Build and install pandas
python setup.py build_ext -j 4
python -m pip install -e . --no-build-isolation --no-use-pep517

Unix / 带有pyenv的MacOS

有关设置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

# Build and install pandas
python setup.py build_ext -j 4
python -m pip install -e . --no-build-isolation --no-use-pep517

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

# Build and install pandas
python setup.py build_ext -j 4
python -m pip install -e . --no-build-isolation --no-use-pep517