NLTK with flask import error

て烟熏妆下的殇ゞ 提交于 2019-12-13 16:24:17

问题


My folder directory is as such

/maindir
  __init__.py
  settings.py
  start
  /run.py
  /venv
    .. other directories for flask here bin,include..etc
  /app
    __init__.py
    main.py
    views.py
    /nbc
      /__init__.py
      naivebayesclassifier.py

The naivebayesclassifier.py module uses the nltk library as such

from nltk.probability import ELEProbDist, FreqDist
import nltk
from collections import defaultdict
from os import listdir  
from os.path import isfile, join

I'm having an issue where if I try to run the program directly from going into /app and running

python main.py 

which further goes on to include nbc and use it, I have no problems.

However when I try to deploy this along with flask. I move one directory out and run ./start, which has the following

. venv/bin.activate
./run.py

and run.py has the following

#!venv/bin/python
from app import app
app.run(debug = True)

This has worked before I included the nltk library, however now it gives me the error saying

ImportError: No module named nltk.probability

I had installed nltk using

sudo pip install -U pyyaml nltk

but I feel I'm missing some installation somewhere to make it work while deploying.


回答1:


It looks like the activation of your virtualenv is causing the problem. Did you activate the virtualenv before running sudo pip install -U pyyaml nltk? If not, they were installed globally. Remember that by default, when you create a virtualenv environment, it will ignore all packages not installed directly into the environment itself (in other words, it will ignore the packages you installed globally using apt-get install). So, you have two options:

  1. Install your dependencies into your virtualenv (by activating the virtualenv then doing pip install nltk). If nltk depends on any development libraries, you will need to install those development libraries as well. Those can be installed using your package manager (apt-get).

  2. Rebuild your virtualenv, this time using the --system-site-packages option. This will allow you to use packages installed outside of the virtualenv environment.



来源:https://stackoverflow.com/questions/17631510/nltk-with-flask-import-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!