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

  • Error: duplicated outer port
  • Kubernetes - Error pulling from a private docker registry
  • Mule performance tuning with limited resources
  • jaas basic authentication jetty-runner
  • docker push to the registry with changed Dockerfile and same tag
  • Accessing Secrets/Private Files Needed for Building in Dockerfile?
  • 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()
    

  • Unable to Connect to Tensorflow Jupyter Notebook in windows 10
  • Rails and docker Postgress Too many connections
  • kafka monitoring using JMX when running kafka in docker in boot2docker
  • docker-compose not recognizing env_file file/location, and still tries to use the default .env
  • Docker use local db for volume
  • Docker registry on EC2
  • 2 Solutions collect form web for “NLTK is not working in docker”

    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.

    final Dockerfile

    FROM python:3.6
    
    ENV NLTK_DATA /usr/share/nltk_data
    
    RUN pip install -U nltk
    RUN pip install -U numpy
    RUN python -m nltk.downloader -d /usr/share/nltk_data all
    
    EXPOSE 80
    
    WORKDIR /var/www
    
    CMD [ "python", "/var/www/main.py" ]
    

    final docker-compose

    nltk:
        build: docker/nltk
        container_name: nltk
        volumes:
          - ./volumes/nltk/var/www/main.py:/var/www/main.py
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.