Skip to content

创建一个 Python 环境

这份指南包含如何通过 envd 配置 Python 环境。如果你还没有用过 envd,请先阅读我们的 教程搭建配置指南

让我们开始 🐍 吧!

指定 Python

envd 默认的语言就是 Python,因此你不需要特意指定语言。或者你可以使用 base 函数来指定。

python
def build():
    base(os="ubuntu20.04", language="python")

envd 中 Python 的默认版本是 3.9(最新的修订号可以参考这里)。如果你需要使用特定版本,只需要用类似 pythonX.Y.Z 的字符串来填充 language 项:

python
def build():
    base(os="ubuntu20.04", language="python3.11")

WARNING

Python2 不被 envd 所支持。

Conda 包

你可以使用 install.conda_packages 函数来安装 conda 包。下面的例子安装了 numpyscipy

python
def build():
    base(os="ubuntu20.04", language="python")
    install.conda_packages(name = [
        "numpy",
        "scipy",
    ])

PyPI 包

你可以使用 install.python_packages 函数来从 PyPI 安装 Python 包。下面的例子安装了 scikit-learnmatplotlib

python
def build():
    base(os="ubuntu20.04", language="python")
    install.python_packages(name = [
        "scikit-learn",
        "matplotlib",
    ])

前面的例子中,envd 使用系统范围内的 pip 来安装 Python 包。

如果 conda 已启用,你也可以使用 install.python_packages 函数来从 PyPI 安装 Python 包。下面的例子里,使用 conda 安装了 numpy and scipy,与此同时,使用 pip 安装了 scikit-learnmatplotlib

python
def build():
    base(os="ubuntu20.04", language="python")
    install.conda_packages(name = [
        "numpy",
        "scipy",
    ])
    install.python_packages(name = [
        "scikit-learn",
        "matplotlib",
    ])

这个例子里,envd 在当前 conda 环境中使用了 pip 来安装包。

指定 shell 程序

你可以通过 shell 函数来指定环境中使用的 shell 程序。下面的例子里使用了 zsh

python
def build():
    base(os="ubuntu20.04", language="python")
    shell("zsh")

指定 VSCode 插件

你可以使用 install.vscode_extensions 函数来指定 VSCode 插件。下面的例子安装了 ms-python.python[1]

python
def build():
    base(os="ubuntu20.04", language="python")
    install.vscode_extensions(["ms-python.python"])

建立 Jupyter notebook

你可以使用 config.jupyter 来建立 Jupyter notebook。接下来的例子里建立了一个 Jupyter notebook:

python
def build():
    base(os="ubuntu20.04", language="python")
    # Use `config.jupyter()` 
    # if you do not need to set up password.
    config.jupyter(token="password")

jupyter

设定 PyPI 索引镜像

PyPI 的镜像或缓存可用于加快本地包安装、允许脱机工作、处理公司防火墙或单纯的网络不稳定。

PyPI 索引镜像可以使用 config.pip_index(url="<index>", extra_url=<extra>) 来设定:

python
config.pip_index(url="https://pypi.tuna.tsinghua.edu.cn/simple")

  1. 因为许可证问题,这里用 open-vsx 替代了 Microsoft VSCode Marketplace。 ↩︎

Released under the Apache-2.0 License. Built with VitePress.