-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8568fbe
commit 7f97141
Showing
19 changed files
with
232 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
.. _django_env_linux: | ||
|
||
======================= | ||
Django开发环境(linux) | ||
======================= | ||
|
||
在 :ref:`centos` 环境上部署Django开发运行环境,基本和 :ref:`django_env_macos` 相同,细节差异: | ||
|
||
- 生产环境采用了古老的CentOS 7.2环境,实际我为了能够追平最新的运行开发环境,采用了 :ref:`build_python3_in_centos7` | ||
- :ref:`virtualenv` 环境采用 :ref:`rebuild_virtualenv` | ||
- 由于数据库后端采用 :ref:`mysql` ,所以需要安装 ``mysqlclient`` 模块 | ||
|
||
构建步骤 | ||
=========== | ||
|
||
- 完成 :ref:`build_python3_in_centos7` 准备工作 | ||
|
||
- 创建 :ref:`virtualenv` (这里项目名为 ``onesre`` ): | ||
|
||
.. literalinclude:: django_mysql/virtualenv_onesre | ||
:caption: 构建一个 ``onesre`` 项目运行环境 | ||
|
||
- 需要安装 mysql 开发包,例如 ``mariadb-devel`` ,安装以后系统会有 ``mysql_config`` 工具: | ||
|
||
.. literalinclude:: django_mysql/install_mysql-devel | ||
:caption: 需要提前安装 ``mysql-devel`` 或 ``mariadb-devel`` 才能执行 ``pip install mysqlclient`` | ||
|
||
- 在项目目录下重新恢复 | ||
|
||
.. literalinclude:: ../../python/startup/rebuild_virtualenv/pip_install_requirements | ||
:language: bash | ||
:caption: 根据requirements.txt恢复virtualenv依赖模块包 | ||
|
||
解决 ``mysqlclient`` 模块安装 | ||
------------------------------ | ||
|
||
这次遇到一个报错,和之前 :ref:`django_mysql` 不同: | ||
|
||
.. literalinclude:: django_env_linux/mysqlclient_error | ||
:caption: ``pip`` 安装 ``mysqlclient`` 报错 | ||
:emphasize-lines: 10-13 | ||
|
||
这里可以看到 ``pkg-config --exists mysqlclient`` 和 ``pkg-config --exists mariadb`` 都是返回 ``1`` (失败)。我手工执行了一下,确实 ``echo $?`` 显示 ``1`` | ||
|
||
仔细一看,原来这个 ``pkg-config`` 是操作系统默认的 ``/usr/bin/pkg-config`` 。这里通过 ``rpm -qf /usr/bin/pkg-config`` 可以看出是属于 ``pkgconfig-0.27.1-4.1.alios7.x86_64`` ,显然不会获得正确的包信息 | ||
|
||
`PyMySQL / mysqlclient / README.md <https://github.com/PyMySQL/mysqlclient/blob/main/README.md>`_ 提供了一个线索: | ||
|
||
.. literalinclude:: django_env_linux/pkg-config_mysqlclient | ||
:caption: 定制编译时指定环境变量 | ||
|
||
既然由于操作系统的 ``pkg-config`` 无法正常工作,那么该如何设置环境变量呢? | ||
|
||
我找了自己部署的一台 :ref:`fedora` 开发环境(吐槽一下公司魔改的CentOS),可以看到: | ||
|
||
.. literalinclude:: django_env_linux/pkg-config_mariadb | ||
:caption: 在正确的环境中执行 ``pkg-config`` 获取环境变量 | ||
:emphasize-lines: 2,4 | ||
|
||
- 所以修正安装方法: | ||
|
||
.. literalinclude:: django_env_linux/pkg-config_mariadb_env | ||
:caption: 设置正确 ``mysqlclient`` 环境变量 | ||
|
||
- 然后再次执行就可以完成: | ||
|
||
.. literalinclude:: ../../python/startup/rebuild_virtualenv/pip_install_requirements | ||
:language: bash | ||
:caption: 根据requirements.txt恢复virtualenv依赖模块包 | ||
|
||
解决 ``pip`` 下载失败(手工下载安装) | ||
------------------------------------- | ||
|
||
墙内的杯具就是,在线安装的灵活方便往往会被GFW干成生不如死: ``pip install`` 过程中,遇到 ``Babel-2.12.1-py3-none-any.whl`` 下载始终中断的问题。解决的方法: :ref:`pip_offline` | ||
|
||
- 通过 ``pip downlaod`` 命令下载指定软件包(版本),这里举例 ``Babel-2.12.1-py3-none-any.whl`` : | ||
|
||
.. literalinclude:: ../../python/startup/pip_offline/pip_download | ||
:caption: ``pip download`` 可以下载指定版本python包 | ||
|
||
- 将下载好的 ``.whl`` python包复制到目标主机,然后就可以直接离线安装: | ||
|
||
.. literalinclude:: ../../python/startup/pip_offline/pip_install_whl | ||
:caption: ``pip install`` 可以安装下载好的 ``.whl`` python包 | ||
|
||
参考 | ||
===== | ||
|
||
- `PyMySQL / mysqlclient / README.md <https://github.com/PyMySQL/mysqlclient/blob/main/README.md>`_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
Collecting mysqlclient (from -r requirements.txt (line 9)) | ||
Using cached mysqlclient-2.2.0.tar.gz (89 kB) | ||
Installing build dependencies ... done | ||
Getting requirements to build wheel ... error | ||
error: subprocess-exited-with-error | ||
|
||
× Getting requirements to build wheel did not run successfully. | ||
│ exit code: 1 | ||
╰─> [25 lines of output] | ||
Trying pkg-config --exists mysqlclient | ||
Command 'pkg-config --exists mysqlclient' returned non-zero exit status 1. | ||
Trying pkg-config --exists mariadb | ||
Command 'pkg-config --exists mariadb' returned non-zero exit status 1. | ||
Traceback (most recent call last): | ||
File "/home/admin/onesre_venv3/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in <module> | ||
main() | ||
File "/home/admin/onesre_venv3/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main | ||
json_out['return_val'] = hook(**hook_input['kwargs']) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
File "/home/admin/onesre_venv3/lib/python3.11/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 118, in get_requires_for_build_wheel | ||
return hook(config_settings) | ||
^^^^^^^^^^^^^^^^^^^^^ | ||
File "/tmp/pip-build-env-92qfmlql/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 355, in get_requires_for_build_wheel | ||
return self._get_build_requires(config_settings, requirements=['wheel']) | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
File "/tmp/pip-build-env-92qfmlql/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 325, in _get_build_requires | ||
self.run_setup() | ||
File "/tmp/pip-build-env-92qfmlql/overlay/lib/python3.11/site-packages/setuptools/build_meta.py", line 341, in run_setup | ||
exec(code, locals()) | ||
File "<string>", line 154, in <module> | ||
File "<string>", line 48, in get_config_posix | ||
File "<string>", line 27, in find_package_name | ||
Exception: Can not find valid pkg-config name. | ||
Specify MYSQLCLIENT_CFLAGS and MYSQLCLIENT_LDFLAGS env vars manually | ||
[end of output] | ||
|
||
note: This error originates from a subprocess, and is likely not a problem with pip. | ||
error: subprocess-exited-with-error | ||
|
||
× Getting requirements to build wheel did not run successfully. | ||
│ exit code: 1 | ||
╰─> See above for output. | ||
|
||
note: This error originates from a subprocess, and is likely not a problem with pip. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
$ pkg-config mariadb --cflags | ||
-I/usr/include/mysql | ||
$ pkg-config mariadb --libs | ||
-lmariadb -ldl -lm -lssl -lcrypto -lz |
2 changes: 2 additions & 0 deletions
2
source/django/startup/django_env_linux/pkg-config_mariadb_env
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export MYSQLCLIENT_CFLAGS="-I/usr/include/mysql" | ||
export MYSQLCLIENT_LDFLAGS="-lmariadb -ldl -lm -lssl -lcrypto -lz" |
3 changes: 3 additions & 0 deletions
3
source/django/startup/django_env_linux/pkg-config_mysqlclient
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export MYSQLCLIENT_CFLAGS=`pkg-config mysqlclient --cflags` | ||
export MYSQLCLIENT_LDFLAGS=`pkg-config mysqlclient --libs` | ||
pip install mysqlclient |
8 changes: 4 additions & 4 deletions
8
source/django/startup/django_env.rst → source/django/startup/django_env_macos.rst
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# yum install mysql-devel | ||
yum install mariadb-devel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/usr/local/bin/python3.11 -m venv onesre_venv3 | ||
|
||
# 这里是因为CentOS 7.2新老python环境混合,清理了旧python版本 | ||
cd ~/onesre_venv3/bin | ||
unlink python | ||
unlink python3 | ||
ln -s /usr/local/bin/python3.11 python3 | ||
ln -s python3 python | ||
|
||
# 激活 | ||
. onesre_venv3/bin/activate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
ps -eo pid,lstart,cmd | grep qemu-system-x86_64 | ||
ps -eo pid,ppid,lstart,cmd | grep qemu-system-x86_64 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
.. _pip_offline: | ||
|
||
===================== | ||
pip离线安装Python包 | ||
===================== | ||
|
||
GFW真是墙内IT工作者的梦魇,倘若需要部署 :ref:`kubernetes` 这样大型基础设施,恐怕一半的精力都要浪费在翻墙、搬运上。即使像 :ref:`django_env_linux` 这样简单的日常开发环境,也不得不祭出"愚公移山"的苦力: | ||
|
||
- 首先找一台网络通畅的主机,最好是同样的Python环境,例如,相同的 :ref:`virtualenv` | ||
|
||
- 通过 ``pip downlaod`` 命令下载指定软件包(版本),这里举例 ``Babel-2.12.1-py3-none-any.whl`` : | ||
|
||
.. literalinclude:: pip_offline/pip_download | ||
:caption: ``pip download`` 可以下载指定版本python包 | ||
|
||
- 将下载好的 ``.whl`` python包复制到目标主机,然后就可以直接离线安装: | ||
|
||
.. literalinclude:: pip_offline/pip_install_whl | ||
:caption: ``pip install`` 可以安装下载好的 ``.whl`` python包 | ||
|
||
|
||
参考 | ||
====== | ||
|
||
- `Installing Python packages (Offline mode) <https://www.ibm.com/docs/en/siffs/2.0.3?topic=python-installing-packages-offline-mode>`_ | ||
- `How do I install a Python package with a .whl file? <https://stackoverflow.com/questions/27885397/how-do-i-install-a-python-package-with-a-whl-file>`_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mkdir -p ~/onesre_venv3/whl/ | ||
pip3.11 download -d ~/onesre_venv3/whl/ Babel==2.12.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# 在目标主机上下载好 .whl 文件,然后执行本地安装 | ||
pip install Babel-2.12.1-py3-none-any.whl |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
source/python/startup/rebuild_virtualenv/pip_install_requirements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
pip install -r source/requirements.txt |