# Python module not found while run from \`docker-compose\` (ok when run from \`docker run\`)

**URL:** <https://forums.docker.com/t/python-module-not-found-while-run-from-docker-compose-ok-when-run-from-docker-run/77061>\
**Category:** Compose\
**Created:** [June 26, 2019, 4:37pm UTC](https://forums.docker.com/t/python-module-not-found-while-run-from-docker-compose-ok-when-run-from-docker-run/77061 "2019-06-26T16:37:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hamiid](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/hamiid/32/24157_2.png) [@hamiid](https://forums.docker.com/u/hamiid)\
**Post date:** [June 26, 2019, 4:37pm UTC](https://forums.docker.com/t/python-module-not-found-while-run-from-docker-compose-ok-when-run-from-docker-run/77061/1 "2019-06-26T16:37:22Z")

</div>

I am running into a very strange behavior.

In a nutshell, importing a python module that’s built from `src` does not work when it’s invoked from `docker-compose`'s `command:` key, however it just works fine if it’s invoked from `docker run`!!!

I came across this while trying [this tutorial](https://docs.docker.com/compose/django/). Here are my files in a directory called `minimal`:

Dockerfile:

```auto
FROM python:2-stretch
RUN mkdir -p /code
WORKDIR /code
ENV PYTHONUNBUFFERED 1
ENV DJANGO_SETTINGS_MODULE composeexample.settings
COPY requirements.txt /code/
RUN pip install -r requirements.txt
COPY . /code/

```

`docker-compose.yml:`

```auto
version: '3'

services:
  db:
    image: postgres
  web:
    build: .
    command: python -c "import haystack" # haystack is pulled from git instead of direct build by pip. see requirements.txt
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

```

`requirements.txt`

```auto
Django==1.5.5
-e git+https://github.com/toastdriven/django-haystack.git@6b67401aea8203fa172ae49a83c2c5356322d8b5#egg=django-haystack
psycopg2==2.5.4
Whoosh==2.6.0

```

This is what I did to test using `compose`:

```auto
docker-compose up --build

```

resulting in:

```auto
Successfully built dacd52422510
Successfully tagged minimal_web:latest
minimal_db_1 is up-to-date
Recreating minimal_web_1 ... done
Attaching to minimal_db_1, minimal_web_1
web_1 | Traceback (most recent call last):
web_1 | File "<string>", line 1, in <module>
web_1 | ImportError: No module named haystack

```

I get the same import error if I do:

```auto
docker-compose run -w /code --entrypoint "bash" web
root@ac0b0bf733be:/code$ python
Python 2.7.16 (default, Jun 11 2019, 02:02:48)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import haystack
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named haystack

```

However if I use `docker run`:

```auto
docker run -ti --rm minimal_web bash
root@9cd69200519e:/code$ python
Python 2.7.16 (default, Jun 11 2019, 02:02:48)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import haystack
>>>

```

Everything seems to be ok.

The error only happens when I try to import `haystack` which is pulled/built from git, which causes a `src` folder be created within container:

```auto
root@344cbf269363:/code# ls -l /code/src
total 8
drwxr-xr-x 8 root root 4096 Jun 26 15:54 django-haystack
-rw-r--r-- 1 root root 185 Jun 26 15:54 pip-delete-this-directory.txt
root@344cbf269363:/code#

```

Attempting to `import` any other module (`django, whoosh, ...`) works just fine in both scenarios.

So what I am missing here? How can I run this successfully using `docker-compose`?

Full code/settings to reproduce the issue is [available here](https://github.com/spamwax/docker_compose_run).  
The image I built is on the hub `hamiid/compose_run:1`

Thanks in advance

```auto
⇒ docker-compose --version
docker-compose version 1.23.2, build 1110ad01

⇒ docker --version
Docker version 18.09.2, build 6247962

macOS 10.13.6

```

**and**

```auto
> docker-compose --version
docker-compose version 1.24.0, build unknown

> docker --version
Docker version 18.09.6-ce, build 481bc77156

> uname -a
Linux manjaro-macpro 4.19.49-1-MANJARO #1 SMP PREEMPT Sun Jun 9 20:24:20 UTC 2019 x86_64 GNU/Linux

```

---

<div class="post-metadata">

**Author:** ![alexeybutyrev](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/alexeybutyrev/32/25838_2.png) [@alexeybutyrev](https://forums.docker.com/u/alexeybutyrev)\
**Post date:** [December 6, 2019, 8:04pm UTC](https://forums.docker.com/t/python-module-not-found-while-run-from-docker-compose-ok-when-run-from-docker-run/77061/2 "2019-12-06T20:04:06Z")

</div>

Try to add module to requirements.txt and run

docker-compose build

---

<div class="post-metadata">

**Author:** ![energetikavirtualization](https://sea3.discourse-cdn.com/docker/user_avatar/forums.docker.com/energetikavirtualization/32/32768_2.png) [@energetikavirtualization](https://forums.docker.com/u/energetikavirtualization)\
**Post date:** [January 29, 2022, 8:41am UTC](https://forums.docker.com/t/python-module-not-found-while-run-from-docker-compose-ok-when-run-from-docker-run/77061/3 "2022-01-29T08:41:08Z")

</div>

@hamiid , Did you solve it? Could you please share your solution. I have the same issue
