Q:

NLTK is not working in docker

I want to run a nltk service in docker.  However I always get the error message "'nltk' is not a package".  Are you able to figure out what is going wrong?  During build everything works fine, the nltk version is printed.  When starting the container with docker-compose up nltk I get

$ docker-compose up nltk

Recreating nltk
Attaching to nltk
nltk          | Traceback (most recent call last):
nltk          |   File "/var/www/nltk.py", line 1, in <module>
nltk          |     from nltk.corpus import brown
nltk          |   File "/var/www/nltk.py", line 1, in <module>
nltk          |     from nltk.corpus import brown
nltk          | ModuleNotFoundError: No module named 'nltk.corpus'; 'nltk' is not a package

docker-compose.yml

nltk:
    build: docker/nltk
    container_name: nltk
    volumes:
      - ./volumes/nltk/var/www/nltk.py:/var/www/nltk.py
    environment:
      HOME: /var/www

Dockerfile

FROM python:3.6

RUN mkdir /var/www
ENV HOME /var/www
WORKDIR /var/www
RUN pip install -U nltk
RUN pip install -U numpy
RUN python -m nltk.downloader -d $HOME/nltk_data all
RUN python -c "import nltk"
RUN python -c "import nltk; print(nltk.__version__)"

EXPOSE 80

CMD [ "python", "/var/www/nltk.py" ]

nltk.py

import nltk
from nltk.corpus import brown

brown.words()

A:

Try renaming nltk.py to something else.  I'm guessing the import nltk and from nltk.corpus is trying to import from your nltk.py file instead of the package.  The reason it works when building the image is because your nltk.py file didn't exist yet, since it is mounted at runtime from the compose file.