安装

在你开始之前

本教程假设您已经按照 安装 Pyramid 除外 不要创建虚拟环境或安装Pyramid . 因此,您将满足以下要求。

  • 操作系统上安装了一个python解释器。

  • 你已经满足了 安装包的要求 .

安装sqlite3及其开发包

如果使用包管理器来安装python,或者从源代码编译python,那么必须安装sqlite3及其开发包。如果您从https://www.python.org下载了作为安装程序的python,那么您已经安装了它,可以跳过此步骤。

如果您需要安装sqlite3包,那么,例如,使用debian系统和 apt-get ,命令如下:

sudo apt-get install libsqlite3-dev

安装 cookiecutter

我们将使用 cookiecutter 从python包项目模板创建python包项目。见 Cookiecutter Installation 以获取指示。

从CookieCutter生成Pyramid项目

我们将在您的UNIX主目录或Windows根目录中创建一个Pyramid项目。假设您知道安装位置的路径 cookiecutter . 发出以下命令并按如下方式覆盖提示中的默认值。

在UNIX上

cd ~
cookiecutter gh:Pylons/pyramid-cookiecutter-starter --checkout main

在Windows上

cd \
cookiecutter gh:Pylons/pyramid-cookiecutter-starter --checkout main

在所有操作系统上

If prompted for the first item, accept the default yes 按回车键。

You've cloned ~/.cookiecutters/pyramid-cookiecutter-starter before.
Is it okay to delete and re-clone it? [yes]: yes
project_name [Pyramid Scaffold]: myproj
repo_name [myproj]: tutorial
Select template_language:
1 - jinja2
2 - chameleon
3 - mako
Choose from 1, 2, 3 [1]: 1
Select backend:
1 - none
2 - sqlalchemy
3 - zodb
Choose from 1, 2, 3 [1]: 2

将目录更改为新创建的项目

在UNIX上

cd tutorial

在Windows上

cd tutorial

设置和使用 VENV 环境变量

我们将设置 VENV 环境变量到虚拟环境的绝对路径,并使用它进行下一步。

在UNIX上

export VENV=~/tutorial

在Windows上

set VENV=c:\tutorial

创建虚拟环境

在UNIX上

python3 -m venv $VENV

在Windows上

python -m venv %VENV%

在虚拟环境中升级打包工具

在UNIX上

$VENV/bin/pip install --upgrade pip setuptools

在Windows上

%VENV%\Scripts\pip install --upgrade pip setuptools

以开发模式安装项目

为了方便地对项目进行开发,您必须在工作区中将项目“注册”为开发鸡蛋。我们将同时安装测试需求。我们用下面的命令来完成。

在UNIX上

$VENV/bin/pip install -e ".[testing]"

在Windows上

%VENV%\Scripts\pip install -e ".[testing]"

在所有操作系统上

控制台将显示 pip 检查包并安装丢失的包。成功执行此命令将显示如下行:

Successfully installed Jinja2-2.11.2 Mako-1.1.3 MarkupSafe-1.1.1 PasteDeploy-2.1.1 Pygments-2.7.3 SQLAlchemy-1.3.22 WebTest-2.0.35 alembic-1.4.3 attrs-20.3.0 beautifulsoup4-4.9.3 coverage-5.3.1 hupper-1.10.2 iniconfig-1.1.1 packaging-20.8 plaster-1.0 plaster-pastedeploy-0.7 pluggy-0.13.1 py-1.10.0 pyparsing-2.4.7 pyramid-1.10.5 pyramid-debugtoolbar-4.9 pyramid-jinja2-2.8 pyramid-mako-1.1.0 pyramid-retry-2.1.1 pyramid-tm-2.4 pytest-6.2.1 pytest-cov-2.10.1 python-dateutil-2.8.1 python-editor-1.0.4 repoze.lru-0.7 six-1.15.0 soupsieve-2.1 toml-0.10.2 transaction-3.0.1 translationstring-1.4 tutorial venusian-3.0.0 waitress-1.4.4 webob-1.8.6 zope.deprecation-4.4.0 zope.interface-5.2.0 zope.sqlalchemy-1.3

测试要求在我们项目的 setup.py 文件,在 tests_requireextras_require 诗节。

25tests_require = [
26    'WebTest',
27    'pytest',
28    'pytest-cov',
29]
49    extras_require={
50        'testing': tests_require,
51    },

使用Alembic初始化和升级数据库

我们使用 Alembic 管理我们的数据库初始化和迁移。

生成第一个修订。

在UNIX上

$VENV/bin/alembic -c development.ini revision --autogenerate -m "init"

在Windows上

%VENV%\Scripts\alembic -c development.ini revision --autogenerate -m "init"

控制台的输出应该如下所示:

2021-01-07 05:15:57,709 INFO  [alembic.runtime.migration:155][MainThread] Context impl SQLiteImpl.
2021-01-07 05:15:57,709 INFO  [alembic.runtime.migration:162][MainThread] Will assume non-transactional DDL.
2021-01-07 05:15:57,712 INFO  [alembic.autogenerate.compare:134][MainThread] Detected added table 'models'
2021-01-07 05:15:57,712 INFO  [alembic.autogenerate.compare:588][MainThread] Detected added index 'my_index' on '['name']'
  Generating <somepath>/tutorial/tutorial/alembic/versions/20210107_d7ab09c3fdec.py ...  done

升级到该版本。

在UNIX上

$VENV/bin/alembic -c development.ini upgrade head

在Windows上

%VENV%\Scripts\alembic -c development.ini upgrade head

控制台的输出应该如下所示:

2021-01-07 05:16:21,558 INFO  [alembic.runtime.migration:155][MainThread] Context impl SQLiteImpl.
2021-01-07 05:16:21,558 INFO  [alembic.runtime.migration:162][MainThread] Will assume non-transactional DDL.
2021-01-07 05:16:21,560 INFO  [alembic.runtime.migration:517][MainThread] Running upgrade  -> d7ab09c3fdec, init

加载默认数据

使用 console script . 键入以下命令,确保您仍在 tutorial 目录 development.ini 在里面):

在UNIX上

$VENV/bin/initialize_tutorial_db development.ini

在Windows上

%VENV%\Scripts\initialize_tutorial_db development.ini

您的控制台不应该有任何输出。您现在应该有一个 tutorial.sqlite 文件保存在当前工作目录中。这是一个SQLite数据库,其中定义了两个表, alembic_versionmodels ,其中每个表都有一条记录。

运行测试

在开发模式和测试需求中安装项目之后,您可以为项目运行测试。以下命令提供选项 pytest 它规定了应为其运行测试的模块,以及 pytest 在安静模式下。

在UNIX上

$VENV/bin/pytest -q

在Windows上

%VENV%\Scripts\pytest -q

对于成功的测试运行,您应该看到这样结束的输出:

.....
5 passed in 0.44 seconds

公开测试覆盖率信息

你可以运行 pytest 命令查看测试覆盖率信息。这与运行测试的方式相同 pytest 有,但提供了额外的 coverage 信息,显示测试覆盖了项目的哪些行。

我们已经安装了 pytest-cov 打包到我们的虚拟环境中,这样我们就可以在覆盖范围内运行测试。

在UNIX上

$VENV/bin/pytest --cov --cov-report=term-missing

在Windows上

%VENV%\Scripts\pytest --cov --cov-report=term-missing

如果成功,您将看到如下输出:

======================== test session starts ========================
platform darwin -- Python 3.9.0, pytest-6.2.1, py-1.10.0, pluggy-0.13.1
rootdir: <somepath>/tutorial, inifile: pytest.ini, testpaths: tutorial, tests
plugins: cov-2.10.1
collected 5 items

tests/test_functional.py ..                                                           [ 40%]
tests/test_views.py ...                                                               [100%]

---------- coverage: platform darwin, python 3.9.0-final-0 -----------
Name                                                 Stmts   Miss  Cover   Missing
----------------------------------------------------------------------------------
tutorial/__init__.py                                     8      0   100%
tutorial/alembic/env.py                                 23      4    83%   28-30, 56
tutorial/alembic/versions/20200106_8c274fe5f3c4.py      12      2    83%   31-32
tutorial/models/__init__.py                             32      2    94%   71, 82
tutorial/models/meta.py                                  5      0   100%
tutorial/models/mymodel.py                               8      0   100%
tutorial/pshell.py                                       7      5    29%   5-13
tutorial/routes.py                                       3      0   100%
tutorial/scripts/__init__.py                             0      0   100%
tutorial/scripts/initialize_db.py                       22     14    36%   15-16, 20-25, 29-38
tutorial/views/__init__.py                               0      0   100%
tutorial/views/default.py                               13      0   100%
tutorial/views/notfound.py                               5      0   100%
----------------------------------------------------------------------------------
TOTAL                                                  138     27    80%

===================== 5 passed in 0.77 seconds ======================

我们的包没有100%的测试覆盖率。

测试和覆盖cookiecutter默认值

pyramid cookiecutter包括的配置默认值 pytest 测试覆盖率。这些配置文件是 pytest.ini.coveragerc ,位于包的根目录。

pytest 跟随 conventions for Python test discovery . 配置默认值来自cookiecutter tell pytest 在哪里找到要在其上运行测试和覆盖的模块。

参见

pytest 的文档 How to invoke pytest 或调用 pytest -h 查看它的全部选项。

启动应用程序

启动应用程序。见 这是什么 pserve 事情 有关更多信息 pserve .

在UNIX上

$VENV/bin/pserve development.ini --reload

在Windows上

%VENV%\Scripts\pserve development.ini --reload

备注

您的操作系统防火墙(如果有的话)可能会弹出一个对话框,请求授权以允许python接受传入的网络连接。

如果成功,您将在控制台上看到类似的内容:

Starting monitor for PID 68932.
Starting server in PID 68932.
Serving on http://localhost:6543
Serving on http://localhost:6543

这意味着服务器已准备好接受请求。

在浏览器中访问应用程序

在浏览器中,访问http://localhost:6543/。您将看到生成的应用程序的默认页面。

您将注意到的一件事是页面右侧的“调试工具栏”图标。您可以在上阅读有关图标用途的更多信息 调试工具栏 . 它允许您在开发时获取有关应用程序的信息。

决定CookiCutter后端选项 sqlalchemy 为你做的

创建项目并选择的后端选项时 sqlalchemy ,cookiecutter作出以下假设:

  • 您愿意将sqlite用于持久存储,尽管几乎任何SQL数据库都可以与sqlAlchemy一起使用。

  • 你愿意用 SQLAlchemy 用于数据库访问工具。

  • 你愿意用 Alembic 用于数据库迁移工具。

  • 您愿意使用 console script 用于数据加载工具。

  • 你愿意用 URL dispatch 将URL映射到代码。

  • 你想用 zope.sqlalchemy, pyramid_tm, 以及 transaction 将会话范围设置为请求的包。

备注

Pyramid 支持任何持久存储机制(例如,对象数据库或文件系统文件)。它还支持将URL映射到代码的附加机制 (traversal )但是,在本教程中,我们将只使用 URL dispatchSQLAlchemy .