[英]Error: No Module Named NLTK Python2.7 Linux
I'm on a Linux Red Hat server and I'm trying to launch a python script. 我在Linux Red Hat服务器上,正在尝试启动python脚本。 I installed nltk, but when I start my script I get the following error: 我安装了nltk,但是当我启动脚本时出现以下错误:
ImportError: No module named nltk
Here are my other python installations: 这是我的其他python安装:
[~/Documents] >which python python2 python3
/bin/python
/bin/python2
However, nltk is installed on: 但是,nltk安装在以下位置:
/usr/lib/python2.7/site-packages
I don't want to change my path variable because I'm not the only one using this server, is there anything else I can do to overcome this problem? 我不想更改路径变量,因为我不是唯一使用此服务器的人,我还能做些什么来克服此问题? Where should I install nltk? 我应该在哪里安装nltk?
Thanks! 谢谢!
EDIT: 编辑:
I added NLTK with sudo pip install nltk 我用sudo pip install nltk添加了NLTK
[~/Documents] >which pip pip2 pip3
/bin/pip
/bin/pip2
pip3: Command not found.
My path: 我自己的路:
[~/Documents] >echo $PATH
/usr/local/java/java/bin:/bin:/usr/bin:
$PYTHONPATH does not exist, I can't install it using only pip $ PYTHONPATH不存在,我无法仅使用pip进行安装
For the permission I think only root can read/write the nltk files: 为了获得许可,我认为只有root才能读取/写入nltk文件:
ls -l
drwxr-x---. 23 root root 4096 Nov 23 12:24 nltk
drwxr-x---. 2 root root 4096 Nov 23 12:24 nltk-3.2.1-py2.7.egg-info
You can add to the begin of you python script 您可以添加到python脚本的开头
import sys; sys.path.append('/usr/lib/python2.7/site-packages')
Edit 1: 编辑1:
this must solve if above does not works: 这必须解决上面的方法不起作用:
import sys
sys.path.insert(0, '/usr/lib/python2.7/site-packages')
Edit 2: 编辑2:
change the path in linux for only when script runs 仅在脚本运行时更改linux中的路径
from subprocess import call
call('export PYTHONPATH='/usr/lib/python2.7/':$PYTHONPATH,shell=True)
or you can call the module direct 或者您可以直接调用该模块
<nltk.pth>
/usr/lib/python2.7/dist-packages
any python modules or packages in the directory will now be importable. 目录中的所有python模块或软件包现在都可以导入。
EDIT 3: 编辑3:
Well after some research maybe there is another way 经过一些研究,也许还有另一种方法
first of all you need to append the directory to yout path with 首先,您需要使用以下命令将目录附加到您的路径
import sys; sys.path.append('/usr/lib/python2.7/site-packages')
you will need importlib 您将需要importlib
from importlib import import_module
Then you may import your module like this: 然后,您可以像这样导入模块:
mod = import_module('nltk')
to get submodules you may do this: 要获取子模块,您可以这样做:
module_you_want = getattr(mod,'module_you_want')
thats it! 而已!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.