问题
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:
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
).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