lakhesis
lakhesis

Reputation: 121

Using nltk without installing

I'm willing to bet the solution to this is going to be extremely simple, but it's driving me crazy.

I am working on linux and using python 3.4.0. I do not have admin privileges. I downloaded and unzipped nltk-3.0.4.

Question: How do I use nltk without installing it?

I have already added the 'mypath/nltk' folder to the "PYTHONPATH" environment variable (confirmed via os.environ['PYTHONPATH'] and sys.path).

But, I am getting the following error when running my script:

Traceback (most recent call last):
  File "run_me.py", line 1, in <module>
    import process_utterances
  File "/h/.../code/process_utterances.py", line 2, in <module>
    import nltk
ImportError: No module named 'nltk'
Error in sys.excepthook:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 62, in apport_excepthook
    import re, traceback
  File "/usr/lib/python3.4/traceback.py", line 3, in <module>
    import linecache
  File "/usr/lib/python3.4/linecache.py", line 10, in <module>
    import tokenize
  File "/h/.../nltk-3.0.4/nltk/tokenize/__init__.py", line 62, in <module>
    from nltk.data              import load
ImportError: No module named 'nltk'

Original exception was:
Traceback (most recent call last):
  File "run_me.py", line 1, in <module>
    import process_utterances
  File "/h/.../code/process_utterances.py", line 2, in <module>
    import nltk
ImportError: No module named 'nltk'

I would greatly appreciate any help in figuring out how to fix this.

Upvotes: 2

Views: 767

Answers (2)

lakhesis
lakhesis

Reputation: 121

The easiest solution:

add a symlink to the nltk folder in the project folder

ln -s /u/.../nltk/

the downloader can be used (through python via nltk.downloader()) to download extra resources

Upvotes: 1

alvas
alvas

Reputation: 122142

In short: The simplest solution is to move the source code for NLTK into your project directory.


In long:

Let myprojectdir be your project directory

alvas@ubi:~$ mkdir myprojectdir
alvas@ubi:~$ cd myprojectdir/

Download and extract NLTK into your project directory

alvas@ubi:~/myprojectdir$ wget https://github.com/nltk/nltk/archive/develop.zip
alvas@ubi:~/myprojectdir$ unzip develop.zip 
alvas@ubi:~/myprojectdir$ ls
develop.zip  nltk-develop
alvas@ubi:~/myprojectdir$ mv nltk-develop/nltk/ .
alvas@ubi:~/myprojectdir$ ls
develop.zip  nltk  nltk-develop
alvas@ubi:~/myprojectdir$ rm -rf nltk-develop/
alvas@ubi:~/myprojectdir$ rm develop.zip

Voila, now you have NLTK in your project.

alvas@ubi:~/myprojectdir$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys, os
>>> import nltk
>>> sys.modules['nltk']
<module 'nltk' from 'nltk/__init__.py'>
>>> os.path.abspath(nltk.__file__)
'/home/alvas/myprojectdir/nltk/__init__.py'

If NLTK is installed natively through pip, then you will see:

alvas@ubi:~/myprojectdir$ cd
alvas@ubi:~$ python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os, sys
>>> import nltk
>>> os.path.abspath(nltk.__file__)
'/usr/local/lib/python2.7/dist-packages/nltk/__init__.pyc'

Upvotes: 1

Related Questions