如何安装 SQLAlchemy

SQLAlchemy 工具包和对象关系映射器是一组广泛的实用程序,用于处理 Python 和数据库。 该工具包提供了一个完整的流行持久性模式包,专为经济而强大的数据库可访问性而设计。 SQLAlchemy 允许开发人员使用简单的 SQL 语句(与其他对象关系映射工具不同),它提供了一种有用的方法来连接数据库表和用户定义的 Python 类。 SQLAlchemy 对象关系映射工具主要以使用 SQL 表达式语言为中心。

SQLAlchemy 交互

SQLAlchemy 有几种直接处理数据库中数据的方法。

  • 原始 SQL – 允许可以为特定目的或数据库开发的自定义、显式、SQL 语句
  • SQL 表达式语言 – 一种使用 Python 构造准确反映关系数据库结构和表达式的语言
  • – 对象关系映射是一种使用面向对象的编程语言在不同系统类型之间转换数据的编码方法

SQLAlchemy 组件

引擎: Engine 是 SQLAlchemy 应用程序的入口,充当 API 和数据库的抽象层。 它与 Dialect 组件和连接池交互以将 SQL 语句从 SQLAlchemy 传送到数据库。

方言:方言是 SQLAlchemy 用于与各种类型的 DBAPI 数据库实现交互的框架。 所有方言都需要安装合适的 DBAPI 驱动程序。 SQLAlchemy 维护多种数据库服务的方言,包括:

  • MySQL
  • 微软 SQL 服务器
  • 甲骨文
  • PostgreSQL
  • SQLite
  • 火鸟
  • Informix
  • 赛贝斯

元数据:元数据由多个 Python 资产组成,这些资产描述了表和其他模式级别的项目。 可以通过显式命名不同的组件及其属性来声明数据库的元数据,使用的结构如下:

  • 桌子
  • 柱子
  • 外键

SQLAlchemy 也可以使用称为反射的过程轻松生成元数据。

飞行前检查

  • 这些说明主要在 Liquid Web 自我管理的 Ubuntu 18.04.3 LTS 专用服务器上以 root 用户身份执行。
  • 这些说明假设您已经安装了 Python 2 和/或 Python 3(版本 3.3 或 >),并在您的服务器上安装了 pip、python3-pip、venv、virtualenv 和 setuptools。 如果您正在寻找有关如何在 CentOS 7 机器上安装 Python 3 的说明,请查看本教程!

如何安装 SQLAlchemy

Python 虚拟环境

首先,让我们看看在 Python 虚拟环境中安装软件。 我们最好在安装 SQLAlchemy 之前创建一个 virtualenv(或 venv,具体取决于您的 Python 版本)。 所以,让我们继续这样做。

[[email protected] ~]# virtualenv sqlalchemy New python executable in sqlalchemy/bin/python Installing distribute.....................done. Installing pip................done [[email protected] ~]# cd sqlalchemy [[email protected] ~]# cd sqlalchemy/  [[email protected] ~]# source bin/activate (sqlalchemy) [email protected]:/home/temp/sqlalchemy#

接下来,安装 SQLAlchemy 的最快方法是使用 Python 包管理器 pip。

[[email protected] ~]# pip3 install sqlalchemy Downloading unpacking sqlalchemy   Downloading SQLAlchemy-0.8.1.tar.gz (3.8Mb) : 3.8Mb downloaded   Running setup.py egg_info for package sqlalchemy   .......     no previously included directories found matching 'doc/build/output' Successfully installed sqlalchemy Cleaning up.... [[email protected] ~]# 

这将限制安装到该虚拟环境,

接下来,我们将演示如何在 CentOS 和 Ubuntu Linux 服务器上安装 SQLAlchemy 和其他必要的软件包。

CentOS

在系统范围内安装 SQLAlchemy 的最简单方法是通过 Python 包管理器 pip。

[[email protected] ~]# which python3 /usr/bin/python3 [[email protected] ~]# python3 -m pip install --upgrade pip (sqlalchemy) [[email protected] sqlalchemy]# pip3 install sqlalchemy Collecting sqlalchemy  Downloading https://files.pythonhosted.org/packages/17/7f/35879c73859368ad19a952b69ee780aa97fc30350dabd45fb948d6a4e3ea/SQLAlchemy-1.3.12.tar.gz (6.0MB)   100% |████████████████████████████████| 6.0MB 290kB/s  Installing collected packages: sqlalchemy  Running setup.py install for sqlalchemy ... done Successfully installed sqlalchemy-1.3.12 (sqlalchemy) [[email protected] sqlalchemy]#

Ubuntu

同样,安装 SQLAlchemy 的最简单方法是通过 pip。

[email protected]:~# apt-get install python-pip Reading package lists... Done Building dependency tree    Reading state information... Done Do you want to continue? [Y/n] y ... ... ... [email protected]:~# 

接下来,我们可以通过 pip 安装 SQLAlchemy。

[email protected]:~# pip3 install SQLAlchemy Collecting SQLAlchemy  Downloading https://files.pythonhosted.org/packages/17/7f/35879c73859368ad19a952b69ee780aa97fc30350dabd45fb948d6a4e3ea/SQLAlchemy-1.3.12.tar.gz (6.0MB)   100% |████████████████████████████████| 6.0MB 102kB/s  Building wheels for collected packages: SQLAlchemy  Running setup.py bdist_wheel for SQLAlchemy ... done  Stored in directory: /root/.cache/pip/wheels/ee/33/44/0788a6e806866ae2e246d5cd841d07498a46bcb3f3c42ea5a4 Successfully built SQLAlchemy Installing collected packages: SQLAlchemy Successfully installed SQLAlchemy-1.3.12 [email protected]:~#

然后,我们可以为 PostgreSQL 和 MySQL 安装各种 DBAPI 驱动程序(python-psycopg2、python-mysqldb)。 如果使用 PostgreSQL 和 MySQL,SQLAlchemy 需要这些模块。

[email protected]:~# apt-get install python-psycopg2 Reading package lists... Done Building dependency tree    Reading state information... Done The following additional packages will be installed:  libpq5 python-egenix-mxdatetime python-egenix-mxtools Suggested packages:  python-egenix-mxdatetime-dbg python-egenix-mxdatetime-doc python-egenix-mxtools-dbg python-egenix-mxtools-doc  python-psycopg2-doc The following NEW packages will be installed:  libpq5 python-egenix-mxdatetime python-egenix-mxtools python-psycopg2 0 upgraded, 4 newly installed, 0 to remove and 25 not upgraded. Need to get 406 kB of archives. After this operation, 1918 kB of additional disk space will be used. Do you want to continue? [Y/n] Y ... ... ... [email protected]:~# 
[email protected]:~# apt-get install python-mysqldb Reading package lists... Done Building dependency tree    Reading state information... Done The following additional packages will be installed:  libmysqlclient20 Suggested packages:  python-mysqldb-dbg The following NEW packages will be installed:  libmysqlclient20 python-mysqldb 0 upgraded, 2 newly installed, 0 to remove and 25 not upgraded. Need to get 738 kB of archives. After this operation, 4187 kB of additional disk space will be used. Do you want to continue? [Y/n] Y ... ... ... [email protected]:~#

现在,我们将安装 MySQL-server 和 PostgreSQL。

[email protected]:~# apt-get install mysql-server Reading package lists... Done Building dependency tree    Reading state information... Done The following NEW packages will be installed:  mysql-server 0 upgraded, 1 newly installed, 0 to remove and 25 not upgraded. Need to get 0 B/9940 B of archives. After this operation, 110 kB of additional disk space will be used. Selecting previously unselected package mysql-server. (Reading database ... 108778 files and directories currently installed.) Preparing to unpack .../mysql-server_5.7.28-0ubuntu0.18.04.4_all.deb ... Unpacking mysql-server (5.7.28-0ubuntu0.18.04.4) ... Setting up mysql-server (5.7.28-0ubuntu0.18.04.4) ... [email protected]:~# 
[email protected]:~# apt-get install postgresql Reading package lists... Done Building dependency tree    Reading state information... Done The following additional packages will be installed:  libsensors4 postgresql-10 postgresql-client-10 postgresql-client-common postgresql-common sysstat Suggested packages:  lm-sensors postgresql-doc locales-all postgresql-doc-10 libjson-perl isag The following NEW packages will be installed:  libsensors4 postgresql postgresql-10 postgresql-client-10 postgresql-client-common postgresql-common sysstat 0 upgraded, 7 newly installed, 0 to remove and 25 not upgraded. Need to get 5209 kB of archives. After this operation, 20.6 MB of additional disk space will be used. Do you want to continue? [Y/n] Y ... ... ... Success. You can now start the database server using:    /usr/lib/postgresql/10/bin/pg_ctl -D /var/lib/postgresql/10/main -l logfile start  Ver Cluster Port Status Owner Data directory Log file 10 main 5432 down postgres /var/lib/postgresql/10/main /var/log/postgresql/postgresql-10-main.log update-alternatives: using /usr/share/postgresql/10/man/man1/postmaster.1.gz to provide /usr/share/man/man1/postmaster.1.gz (postmaster.1.gz) in auto mode Setting up postgresql (10+190ubuntu0.1) ... Processing triggers for man-db (2.8.3-2ubuntu0.1) ... Processing triggers for ureadahead (0.100.0-21) ... Processing triggers for libc-bin (2.27-3ubuntu1) ... Processing triggers for systemd (237-3ubuntu10.33) ... [email protected]:~#

现在,我们将安装 sqlite3 命令行界面。

[email protected]:~# apt-get install sqlite3 Reading package lists... Done Building dependency tree    Reading state information... Done Suggested packages:  sqlite3-doc The following NEW packages will be installed:  sqlite3 0 upgraded, 1 newly installed, 0 to remove and 25 not upgraded. Need to get 754 kB of archives. After this operation, 2481 kB of additional disk space will be used. Get:1 https://us.archive.ubuntu.com/ubuntu bionic-updates/main amd64 sqlite3 amd64 3.22.0-1ubuntu0.2 [754 kB] Fetched 754 kB in 0s (2132 kB/s) Selecting previously unselected package sqlite3. (Reading database ... 109582 files and directories currently installed.) Preparing to unpack .../sqlite3_3.22.0-1ubuntu0.2_amd64.deb ... Unpacking sqlite3 (3.22.0-1ubuntu0.2) ... Setting up sqlite3 (3.22.0-1ubuntu0.2) ... Processing triggers for man-db (2.8.3-2ubuntu0.1) ... [email protected]:~# 

接下来,我们运行“apt-get install python3-dev”命令来安装包含 Python.h 的包。 这是一个头文件,允许我们为 SQLAlchemy 安装 C 扩展加速。

[email protected]:~# apt-get install python3-dev Reading package lists... Done Building dependency tree    Reading state information... Done The following additional packages will be installed:  libpython3-dev libpython3.6-dev python3.6-dev The following NEW packages will be installed:  libpython3-dev libpython3.6-dev python3-dev python3.6-dev 0 upgraded, 4 newly installed, 0 to remove and 25 not upgraded. Need to get 45.3 MB of archives. After this operation, 76.7 MB of additional disk space will be used. Do you want to continue? [Y/n] y ... ... ... Processing triggers for man-db (2.8.3-2ubuntu0.1) ... [email protected]:~# 

现在,运行“python3-setuptools”的安装程序。

[email protected]:~/sqlalchemy# apt-get install -y python3-setuptools Reading package lists... Done Building dependency tree    Reading state information... Done Suggested packages:  python-setuptools-doc The following NEW packages will be installed:  python3-setuptools 0 upgraded, 1 newly installed, 0 to remove and 25 not upgraded. Need to get 248 kB of archives. After this operation, 1319 kB of additional disk space will be used. Get:1 https://us.archive.ubuntu.com/ubuntu bionic/main amd64 python3-setuptools all 39.0.1-2 [248 kB] Fetched 248 kB in 0s (802 kB/s)      Selecting previously unselected package python3-setuptools. (Reading database ... 109588 files and directories currently installed.) Preparing to unpack .../python3-setuptools_39.0.1-2_all.deb ... Unpacking python3-setuptools (39.0.1-2) ... Setting up python3-setuptools (39.0.1-2) ... [email protected]:~/sqlalchemy#

接下来,让我们 cd 进入 sqlalchemy 目录并运行 setup.py

[email protected]:~/sqlalchemy# python3 setup.py install running install running bdist_egg running egg_info ... ... ... Installed /usr/local/lib/python3.6/dist-packages/SQLAlchemy-1.4.0b1.dev0-py3.6-linux-x86_64.egg Processing dependencies for SQLAlchemy==1.4.0b1.dev0 Finished processing dependencies for SQLAlchemy==1.4.0b1.dev0 [email protected]:~/sqlalchemy# 

验证 SQLAlchemy 的版本

最后,我们可以验证安装。

[[email protected] ~]# python3 Python 3.6.8 (default, Aug 7 2019, 17:28:10)  [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import sqlalchemy >>> sqlalchemy.__version__ '1.3.12' >>> exit() [[email protected] ~]#

使用 Git 安装

我们将介绍的最后一种安装方法是使用 git。

[email protected]:~# git clone https://github.com/sqlalchemy/sqlalchemy.git Cloning into 'sqlalchemy'... remote: Enumerating objects: 98, done. remote: Counting objects: 100% (98/98), done. remote: Compressing objects: 100% (65/65), done. remote: Total 154689 (delta 37), reused 81 (delta 20), pack-reused 154591 Receiving objects: 100% (154689/154689), 41.14 MiB | 32.56 MiB/s, done. Resolving deltas: 100% (116631/116631), done. [email protected]:~# cd sqlalchemy/ [email protected]:~/sqlalchemy# python setup.py install running install running bdist_egg running egg_info ... ... ... Installed /usr/local/lib/python2.7/dist-packages/SQLAlchemy-1.4.0b1.dev0-py2.7-linux-x86_64.egg Processing dependencies for SQLAlchemy==1.4.0b1.dev0 Finished processing dependencies for SQLAlchemy==1.4.0b1.dev0 [email protected]:~/sqlalchemy# [email protected]:~/sqlalchemy# python Python 2.7.17 (default, Nov  7 2019, 10:07:09)  [GCC 7.4.0] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import sqlalchemy >>> sqlalchemy.__version__ '1.4.0b1

为了验证安装并检查我们的 SQLAlchemy 版本,我们将运行以下命令。

[email protected]:~/sqlalchemy# python Python 2.7.17 (default, Nov  7 2019, 10:07:09)  [GCC 7.4.0] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import sqlalchemy >>> sqlalchemy.__version__ '1.4.0b1 [email protected]:~/sqlalchemy# 

就是这样! 安装了 SQLAlchemy。

附加工具

SQLAlchemy 还包括大量的 附加工具和附件 扩展您的图书馆和资源,如这里所示。

现在还有时间订购!

要获得有利于 SQLAlchemy 的稳定、耐用和尖端平台,请立即联系我们以查看我们可用的全系列专用服务器。 作为奖励,您可以以优惠价格购买安全服务器!

给我们打电话 800.580.4985,或打开一个 聊天 或与我们联系,与我们知识渊博的解决方案或经验丰富的托管顾问之一交谈,以了解您今天如何利用这项技术!