设置使用Python 3.9 + Poetry配置Docker时,出现’不包含任何元素’错误。

huangapple go评论68阅读模式
英文:

setting up docker with python 3.9 + poetry getting 'does not contain any element'

问题

I am trying to setup docker for local dev and i keep getting "does not contain any element" when i try to use poetry with docker. Originally I used the requirement.txt, however i would prefer it to have poetry because where i work we have a pyserver which we need to pull some of the .wh from.

So i am trying to keep it basic. This my structure and some basic code

-------------------main.py-------------

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def root():
    return {"message": "Hello, World!"}

----------------------uvicorn_conf.py------

from uvicorn.workers import UvicornWorker
bind = "0.0.0.0:8000"
workers = 4
worker_class = UvicornWorker

-----------Dockerfile-------

FROM python:3.9

# Set the working directory to /app
WORKDIR /app

# Copy the entire project directory to the container
COPY . .

# Install Poetry and project dependencies
RUN pip install poetry
RUN poetry config virtualenvs.create false
RUN poetry install --no-dev

# Start the server
CMD ["poetry", "run", "start"]

---------pyproject.toml------

[tool.poetry]
name = "my-app"
version = "0.1.0"
description = ""
authors = ["py_dev <test@testing.com>"]
readme = "README.md"
packages = [{include = "my_app"}]

[tool.poetry.dependencies]
python = "^3.9"
fastapi = "^0.95.1"
uvicorn = "^0.21.1"


[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.poetry.scripts]
start = "uvicorn my_app.main:app --config uvicorn_conf.py"
英文:

I am trying to setup docker for local dev and i keep getting "does not contain any element" when i try to use poetry with docker. Originally I used the requirement.txt, however i would prefer it to have poetry because where i work we have a pyserver which we need to pull some of the .wh from.

So i am trying to keep it basic. This my structure and some basic code

设置使用Python 3.9 + Poetry配置Docker时,出现’不包含任何元素’错误。

-------------------main.py-------------

from fastapi import FastAPI

app = FastAPI()

@app.get(&quot;/&quot;)
async def root():
    return {&quot;message&quot;: &quot;Hello, World!&quot;}

----------------------uvicorn_conf.py------

from uvicorn.workers import UvicornWorker
bind = &quot;0.0.0.0:8000&quot;
workers = 4
worker_class = UvicornWorker

-----------Dockerfile-------

FROM python:3.9

# Set the working directory to /app
WORKDIR /app

# Copy the entire project directory to the container
COPY . .

# Install Poetry and project dependencies
RUN pip install poetry
RUN poetry config virtualenvs.create false
RUN poetry install --no-dev

# Start the server
CMD [&quot;poetry&quot;, &quot;run&quot;, &quot;start&quot;]

---------pyproject.toml------

[tool.poetry]
name = &quot;my-app&quot;
version = &quot;0.1.0&quot;
description = &quot;&quot;
authors = [&quot;py_dev &lt;test@testing.com&gt;&quot;]
readme = &quot;README.md&quot;
packages = [{include = &quot;my_app&quot;}]

[tool.poetry.dependencies]
python = &quot;^3.9&quot;
fastapi = &quot;^0.95.1&quot;
uvicorn = &quot;^0.21.1&quot;


[build-system]
requires = [&quot;poetry-core&quot;]
build-backend = &quot;poetry.core.masonry.api&quot;

[tool.poetry.scripts]
start = &quot;uvicorn my_app.main:app --config uvicorn_conf.py&quot;

And every time i run build, I get this error. 设置使用Python 3.9 + Poetry配置Docker时,出现’不包含任何元素’错误。
I am not sure how to fix this. Any advice or direction on how to get it working would be brilliant. Thank you in advance.

答案1

得分: 1

没有在您发布的截图中找到“my_app”目录,所以您应该在您的pyproject.toml文件中将"my_app"更新为"app"

packages = [{include = "app"}]

更多信息,请参见https://python-poetry.org/docs/pyproject/#packages。

英文:

There's no "my_app" directory in the screenshot you posted, so you should update &quot;my_app&quot; to &quot;app&quot; in your pyproject.toml:

packages = [{include = &quot;app&quot;}]

See https://python-poetry.org/docs/pyproject/#packages for more info.

huangapple
  • 本文由 发表于 2023年4月17日 01:08:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76029219.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定