Azure / Django / Celery / Ubuntu | tkinter & libtk8.6.so 导入问题

Azure / Django / Celery / Ubuntu | tkinter & libtk8.6.so import issue

更新/解决方案 根据 Sytech 的回答....

没有意识到构建在 Ubuntu 中,其中包含所有包,但是当 Azure 将其部署到 Linux 容器时,缺少所需的包。

与其他 questions/answers 一样,只需将这些安装添加到 Azure 将使用的启动脚本 例如

#!/bin/bash
apt-get update
apt-get install tk --yes
python manage.py wait_for_db
python manage.py migrate
gunicorn --bind=0.0.0.0 --timeout 600 app.wsgi --access-logfile '-' --error-logfile '-' &
celery -A app worker -l info --uid=1

原版Post: 当 Azure 构建并部署 Python3.9 Django/Django-Rest WebApp 时,它在启动时一直失败。

有问题的错误(完整日志如下)

2022-03-08T21:13:30.385999188Z   File "/tmp/8da0147da65ec79/core/models.py", line 1, in <module>

2022-03-08T21:13:30.386659422Z     from tkinter import CASCADE

2022-03-08T21:13:30.387587669Z   File "/opt/python/3.9.7/lib/python3.9/tkinter/__init__.py", line 37, in <module>

2022-03-08T21:13:30.387993189Z     import _tkinter # If this fails your Python may not be configured for Tk

2022-03-08T21:13:30.388227101Z ImportError: libtk8.6.so: cannot open shared object file: No such file or directory

我遇到了其他答案,需要确保 tkinter 安装了 sudo apt-get python3-tk,我已将其添加到部署 yml 文件

不过好像还是有问题。 恢复到以前的部署代码成功,应用程序中唯一添加的功能是Celery。不知道跟这个有没有关系。

我添加 tk/tkinter 安装的顺序是否错误?

当我恢复到以前的代码并成功时 build/deploy 我 ssh 进入容器并 运行 python shell 并尝试手动导入 tkinter模块。

Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/opt/python/3.9.7/lib/python3.9/tkinter/__init__.py", line 37, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError: libtk8.6.so: cannot open shared object file: No such file or directory

它像预期的那样出错。

当我运行apt-get update&&apt-get install python3-tk --yes手动在容器里,然后回到shell容器上导入tkinter没有报错。

为什么我认为某些东西没有安装在正确的位置?虚拟环境?或者在构建过程中被覆盖?

build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Set up Python version
        uses: actions/setup-python@v1
        with:
          python-version: "3.9"

      - name: Create and start virtual environment
        run: |
          python -m venv venv
          source venv/bin/activate

      - name: Install TK dependency
        run: |
          sudo apt-get update
          sudo apt-get install python3-tk

      - name: Install dependencies
        run: pip install -r requirements.txt

      - name: Upload artifact for deployment jobs
        uses: actions/upload-artifact@v2
        with:
          name: python-app
          path: |
            . 
            !venv/

App Log吐在下面...

2022-03-08T21:13:27.830330743Z Updated PYTHONPATH to ':/opt/startup/code_profiler:/tmp/8da0147da65ec79/antenv/lib/python3.9/site-packages'

2022-03-08T21:13:30.370903021Z Traceback (most recent call last):

2022-03-08T21:13:30.371872470Z   File "/tmp/8da0147da65ec79/manage.py", line 22, in <module>

2022-03-08T21:13:30.372648510Z     main()

2022-03-08T21:13:30.373176037Z   File "/tmp/8da0147da65ec79/manage.py", line 18, in main

2022-03-08T21:13:30.373892773Z     execute_from_command_line(sys.argv)

2022-03-08T21:13:30.374862922Z   File "/tmp/8da0147da65ec79/antenv/lib/python3.9/site-packages/django/core/management/__init__.py", line 446, in execute_from_comma
nd_line

2022-03-08T21:13:30.374880323Z     utility.execute()

2022-03-08T21:13:30.378586012Z   File "/tmp/8da0147da65ec79/antenv/lib/python3.9/site-packages/django/core/management/__init__.py", line 420, in execute

2022-03-08T21:13:30.378603012Z     django.setup()

2022-03-08T21:13:30.378607713Z   File "/tmp/8da0147da65ec79/antenv/lib/python3.9/site-packages/django/__init__.py", line 24, in setup

2022-03-08T21:13:30.378612113Z     apps.populate(settings.INSTALLED_APPS)

2022-03-08T21:13:30.378679216Z   File "/tmp/8da0147da65ec79/antenv/lib/python3.9/site-packages/django/apps/registry.py", line 116, in populate

2022-03-08T21:13:30.378689817Z     app_config.import_models()

2022-03-08T21:13:30.378694417Z   File "/tmp/8da0147da65ec79/antenv/lib/python3.9/site-packages/django/apps/config.py", line 304, in import_models

2022-03-08T21:13:30.379003533Z     self.models_module = import_module(models_module_name)

2022-03-08T21:13:30.381756173Z   File "/opt/python/3.9.7/lib/python3.9/importlib/__init__.py", line 127, in import_module

2022-03-08T21:13:30.383257849Z     return _bootstrap._gcd_import(name[level:], package, level)

2022-03-08T21:13:30.383423757Z   File "<frozen importlib._bootstrap>", line 1030, in _gcd_import

2022-03-08T21:13:30.383857479Z   File "<frozen importlib._bootstrap>", line 1007, in _find_and_load

2022-03-08T21:13:30.384148694Z   File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked

2022-03-08T21:13:30.384836329Z   File "<frozen importlib._bootstrap>", line 680, in _load_unlocked

2022-03-08T21:13:30.384850030Z   File "<frozen importlib._bootstrap_external>", line 850, in exec_module

2022-03-08T21:13:30.385281052Z   File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed

2022-03-08T21:13:30.385999188Z   File "/tmp/8da0147da65ec79/core/models.py", line 1, in <module>

2022-03-08T21:13:30.386659422Z     from tkinter import CASCADE

2022-03-08T21:13:30.387587669Z   File "/opt/python/3.9.7/lib/python3.9/tkinter/__init__.py", line 37, in <module>

2022-03-08T21:13:30.387993189Z     import _tkinter # If this fails your Python may not be configured for Tk

2022-03-08T21:13:30.388227101Z ImportError: libtk8.6.so: cannot open shared object file: No such file or directory
2022-03-08T21:13:36.193Z ERROR - Container <container_name>_0_fd6a978c for site <container_name> has exited, failing site start

Tkinter 已包含在 ubuntu-latest 映像中。无需特殊设置。

jobs:
  verify-tkinter:
    name: verify-tkinter
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Set up Python version
        uses: actions/setup-python@v1
        with:
          python-version: "3.9"
      - name: show tk version
        run: |
          python -c "import tkinter;print(tkinter.TkVersion)"

如果在部署后出现此错误,您需要在部署环境中安装 tkinter,这与 GitHub 操作运行是分开的。

在您的服务器上 运行 Ubuntu 20,确保安装了 tk 包,它提供了所需的 libtk8.6.so 文件。

apt install -y tk