如何将 mysqlclient 添加到 Poetry 环境

问题描述 投票:0回答:2

我正在创建一个项目,该项目需要将运行在 docker 容器中的 Python 连接到运行在另一个容器中的 MySQL 数据库。目前,我的

docker-compose
文件如下所示:

version: "3"

services:

  login:
    build:
      context: ./services/login
      dockerfile: docker/Dockerfile
    ports:
      - "80:80"
    # Need to remove this volume - this is only for dev work
    volumes:
      - ./services/login/app:/app
    # Need to remove this command - this is only for dev work
    command: /start-reload.sh

  db_users:
    image: mysql
    volumes:
      - ./data/mysql/users_data:/var/lib/mysql
      - ./databases/users:/docker-entrypoint-initdb.d/:ro
    restart: always
    ports:
      - 3306:3306
    # Remove 'expose' below for prod
    expose:
      - 3306
    environment:
      MYSQL_ROOT_PASSWORD: password
      MYSQL_DATABASE: users
      MYSQL_USER: user
      MYSQL_PASSWORD: password

我的

Dockerfile
login
服务看起来像这样:

# Note: this needs to be run from parent service directory

FROM tiangolo/uvicorn-gunicorn-fastapi:python3.8

# Install Poetry
RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | POETRY_HOME=/opt/poetry python && \
    cd /usr/local/bin && \
    ln -s /opt/poetry/bin/poetry && \
    poetry config virtualenvs.create false

# Copy using poetry.lock* in case it doesn't exist yet
COPY ./app/pyproject.toml ./app/poetry.lock* /app/

RUN poetry install --no-root --no-dev

COPY ./app /app

我正在尝试将我的

login
服务连接到
db_users
,并想使用 mysqlclient,但是当我运行
poetry add mysqlclient
时,我收到一个错误,其中包括以下几行:

    /bin/sh: mysql_config: command not found
    /bin/sh: mariadb_config: command not found
    /bin/sh: mysql_config: command not found
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/private/var/folders/33/5yy7bny964bb0f3zggd1b4440000gn/T/pip-req-build-lak6lqu7/setup.py", line 15, in <module>
        metadata, options = get_config()
      File "/private/var/folders/33/5yy7bny964bb0f3zggd1b4440000gn/T/pip-req-build-lak6lqu7/setup_posix.py", line 70, in get_config
        libs = mysql_config("libs")
      File "/private/var/folders/33/5yy7bny964bb0f3zggd1b4440000gn/T/pip-req-build-lak6lqu7/setup_posix.py", line 31, in mysql_config
        raise OSError("{} not found".format(_mysql_config_path))
    OSError: mysql_config not found
    mysql_config --version
    mariadb_config --version
    mysql_config --libs
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

我假设这与我需要 mysql-connector-c 库才能工作这一事实有关,但我不确定如何在诗歌中实现这一点。

我正在查看以下this教程,但由于我不是在本地运行 MySQL 而是在 docker 中运行,所以我不确定如何将这些步骤转换为在 docker 中工作。

所以基本上,我的问题有两个方面:

  1. 如何将
    mysqlclient
    添加到我的
    pyproject.toml
    文件
  2. 我如何让它在我的 docker env 中工作?
python mysql docker mysql-python python-poetry
2个回答
0
投票

我忘记了我的开发环境也在 Docker 中,所以我真的不需要关心诗歌环境。

话虽如此,我将 Dockerfile 编辑为如下所示:

# Note: this needs to be run from parent service directory

FROM tiangolo/uvicorn-gunicorn-fastapi:python3.8

RUN apt-get install default-libmysqlclient-dev

# Install Poetry
RUN curl -sSL https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py | POETRY_HOME=/opt/poetry python && \
    cd /usr/local/bin && \
    ln -s /opt/poetry/bin/poetry && \
    poetry config virtualenvs.create false

# Copy using poetry.lock* in case it doesn't exist yet
COPY ./app/pyproject.toml ./app/poetry.lock* /app/

RUN poetry install --no-root --no-dev

COPY ./app /app

现在一切都按预期工作了。


0
投票

对于所有其他因为无法将 mysqlclient 添加到诗歌中而被 google 引导到这里的人:

问题是 mysqlclient 不是您要查找的 python 包的名称。您需要运行以下命令:

poetry add mysql-connector-python
© www.soinside.com 2019 - 2024. All rights reserved.