警告:“news”是 pyproject.toml 中定义的入口点,但它没有作为脚本安装。你可能会得到不正确的`sys.argv[0]`

问题描述 投票:0回答:3
  • 我正在尝试使用 docker compose 在 docker 中运行我的基于诗歌的 python 项目
  • 当我运行该应用程序时,它可以工作,但它给了我这个警告
ch_news_dev_python          | Warning: 'news' is an entry point defined in pyproject.toml, but it's not installed as a script. You may get improper `sys.argv[0]`.
ch_news_dev_python          | 
ch_news_dev_python          | The support to run uninstalled scripts will be removed in a future release.
ch_news_dev_python          | 
ch_news_dev_python          | Run `poetry install` to resolve and get rid of this message.

我的项目结构

news
├── docker
│   ├── development
│   │   ├── ...
│   │   ├── python_server
│   │   │   └── Dockerfile
│   │   ├── .env
│   │   └── docker-compose.yml
│   ├── production
│   │   └── ...
│   └── test
│       └── ...
├── src
│   └── news
│       ├── __init__.py
│       ├── __main__.py
│       ├── app.py
│       └── ...
├── tests
├── .gitignore
├── pyproject.toml
├── poetry.lock
└── ...

我的python_server/Dockerfile

FROM python:3.10.11-slim

ENV PYTHONDONTWRITEBYTECODE 1 \
    PYTHONUNBUFFERED 1

RUN apt-get update \
    && apt-get install --no-install-recommends -y gcc libffi-dev g++\
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

ENV POETRY_VERSION=1.5.0

RUN pip install "poetry==$POETRY_VERSION"

RUN groupadd --gid 10000 ch_news \
    && useradd --uid 10000 --gid ch_news --shell /bin/bash --create-home ch_news

WORKDIR /home/ch_news

COPY --chown=10000:10000 pyproject.toml poetry.lock ./

USER ch_news

RUN poetry install --no-root --no-ansi --without dev

COPY --chown=10000:10000 ./src ./

CMD ["poetry", "run", "news"]

我的 docker-compose 文件

version: '3.9' # optional since v1.27.0
name: ch_news_dev
services:
  ...

  ch_news_dev_python:
    build:
      context: ../..
      dockerfile: ./docker/development/python_server/Dockerfile
    container_name: ch_news_dev_python
    depends_on:
      ch_news_dev_postgres:
        condition: service_healthy
    env_file:
      - .env
    image: ch_news_dev_python_image
    networks:
      - network
    restart: 'always'
    volumes:
      - postgres_certs:/home/ch_news/certs

networks:
  network:
    driver: bridge

volumes:
  postgres_certs:
    driver: local
  postgres_data:
    driver: local

我的pyproject.toml文件

[tool.poetry]
authors = ["..."]
description = "..."
name = "news"
version = "0.1.0"

[tool.poetry.dependencies]
feedparser = "^6.0.10"
python = "^3.10"
aiohttp = "^3.8.4"
python-dateutil = "^2.8.2"
asyncpg = "^0.27.0"
loguru = "^0.7.0"

[tool.poetry.dev-dependencies]
commitizen = "^3.2.2"
pre-commit = "^3.3.2"
pytest = "^7.3.1"
pytest-cov = "^4.0.0"
tox = "^4.5.1"

bandit = "^1.7.5"
black = "^23.3.0"
darglint = "^1.8.1"
flake8 = "^6.0.0"
flake8-bugbear = "^23.5.9"
flake8-docstrings = "^1.7.0"
isort = "^5.12.0"
mypy = "^1.3.0"
pytest-clarity = "^1.0.1"
pytest-sugar = "^0.9.7"
typeguard = "^4.0.0"
xdoctest = "^1.1.0"
aioresponses = "^0.7.4"
pytest-asyncio = "^0.21.0"
types-python-dateutil = "^2.8.19"

[tool.poetry.group.dev.dependencies]
isort = "^5.12.0"
types-python-dateutil = "^2.8.19.7"
flake8-docstrings = "^1.7.0"
xdoctest = "^1.1.1"
pre-commit = "^3.3.2"
commitizen = "^3.2.2"
tox = "^4.5.1"
mypy = "^1.3.0"
pytest = "^7.3.1"
flake8-bugbear = "^23.5.9"
black = "^23.3.0"
pytest-asyncio = "^0.21.0"
bandit = "^1.7.5"
typeguard = "^4.0.0"
pytest-sugar = "^0.9.7"

[tool.coverage.run]
branch = true
omit = ["src/news/__main__.py", "src/news/app.py"]
source = ["news"]

[tool.pytest.ini_options]
pythonpath = "src"
addopts = [
    "--import-mode=importlib",
]

[tool.coverage.report]
fail_under = 95

[tool.isort]
profile = "black"
src_paths = ["src", "tests"]
skip_gitignore = true
force_single_line = true
atomic = true
color_output = true

[tool.mypy]
pretty = true
show_column_numbers = true
show_error_codes = true
show_error_context = true
ignore_missing_imports = true
strict = true
warn_unreachable = true

[tool.poetry.scripts]
news = "news.__main__:app"

[tool.commitizen]
name = "cz_conventional_commits"
tag_format = "v$major.$minor.$patch$prerelease"
version = "0.0.1"
[build-system]
build-backend = "poetry.core.masonry.api"
requires = ["poetry-core>=1.0.0"]

有人可以告诉我如何摆脱这个警告吗?

更新1

即使删除 --no-root 后仍收到警告

python docker dockerfile python-packaging python-poetry
3个回答
1
投票

该错误与诗歌中声明的入口点有关 在你的文件 pyproject.toml 中:

[tool.poetry.scripts]
news = "news.__main__:app"

声明入口点后,必须执行命令

poetry install

在您的终端中


0
投票

项目中 src 目录的名称应与 toml 中的名称相同(pyproject.toml 的第 4 行)

所以你应该将你的 src 目录重命名为“news”。或者(作为一个选项)您可以将 toml 第 4 行中的 'name = "news"' 更改为 'name = "src"'。这对我有用。


-2
投票

您的 docker 文件显示,您正在使用

poetry install
标志运行
--no-root
。入口点是包的一部分,因此只有在安装包时才会安装它们。您必须在没有
poetry install
标志的情况下运行
--no-root

我看不出你有什么理由使用

--no-root
。在不安装项目本身的情况下,您的
import
语句能够正常工作只是运气好。

© www.soinside.com 2019 - 2024. All rights reserved.