Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

540
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!