Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

541
Vistas
python and docker for module import errors, which is possible to work for both?

current i have a fastapi app that i am developing and try to do testing under 2 machine. 1 with docker and another one without.

in this case without docker one, i wanted to launch uvicorn from the terminal as usual

uvicorn main:app --reload

is possible to run without issue, with my main.py having imports from another file like this

from util import search_card_price

with the same thing, i went for docker and try to build it, it will give me this error

web_1      |   File "./app/main.py", line 6, in <module>

web_1      |     from util import search_card_price

web_1      | ModuleNotFoundError: No module named 'util'

what is the best way for me to do my import that works on both places?

my current application is something like for the structure,

fastapi app main folder
|
->app
  |
  -> main.py
  -> util.py
  -> config.py
  -> test.py

i know if using something like this will work for docker

from .util import search_card_price

but with this solution, is not working on my local terminal run without docker. i need to know how to solve both at the same time

here is my dockerfile

# Dockerfile

# pull the official docker image
FROM python:3.9.4-slim

# set work directory
WORKDIR /app

# set env variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install dependencies
COPY requirements.txt .
RUN pip install -r requirements.txt

# copy project
COPY . .

and docker compose

version: '3.8'

services:
  web:
    build: .
    command: bash -c 'uvicorn app.main:app --host 0.0.0.0'
    volumes:
      - .:/app
    expose:
      - 8000
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.fastapi.rule=Host(`testing.localhost`)"

  traefik:
    image: traefik:v2.2
    ports:
      - 80:80
      - 8081:8080
    volumes:
      - "./traefik.dev.toml:/etc/traefik/traefik.toml"
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

If the Dockerfile is outside the app directory, you are copying the directory too the /app inside the docker. That moves the util.py to /app/app/util.py inside docker. You can use

# copy project
COPY .app /app/

to skip creating another app in /app.

You may need remove app. from the command as well.

command: bash -c 'uvicorn main:app --host 0.0.0.0'


over 4 years ago · Santiago Trujillo Denunciar

0

actually it turns out i can use relative import

from .util import search_card_price

for local run as well, the relative import works on docker but not on my localhost here without docker is because i am using this in my terminal to start fastapi

uvicorn main:app --reload

but now, if i retain with relative import and use this command instead

uvicorn app.main:app --reload

adding app. infront of the main and execute this command in my root project folder, everything works fine for my local with relative import it seems.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda