cannot import wordnet from python code - python-2.7

I want to access wordnet from my python code. I am using the following code for this purpose:
from nltk.corpus import wordnet as wn
word=wn.synsets('dog')
print word
I get no error but no output also.The same code on python interpreter gives the correct output. can anyone suggest some method to resolve this problem.

Related

pep8 gives strange failures when typing module is combined with matplotlib or tensorflow

I'm getting a strange errors when running pep8 in Python2.7 on files using the typing module.
If I make a file my_file.py containing
import typing
import tensorflow
Then py.test -k pep8 my_file.py gives Segmentation fault (core dumped). Removing either of the two imports, or reversing their order, stops the error. This only seems to happen in pep8, not when running normally in Python.
It causes a different error with matplotlib:
import typing
import matplotlib
Here, py.test -k pep8 pepcheck_file_a.py gives E RuntimeError: maximum recursion depth exceeded while calling a Python object.
As I mentioned, in either case, reversing the import order to:
import tensorflow
import typing
Stops the error.
It's annoying in a large codebase to have to make sure that typing is always imported after various other packages (one has to manually rearrange the imports in each file).
Is there some easier solution to this?

Caffe Framework :Importing Error

I followed the instructions from "https://gist.github.com/nikitametha/c54e1abecff7ab53896270509da80215" and installed caffe framework.
But ,when I import caffe from some other directory ,it is showing this error
caffe framework was installed but, it still shows the import error -
No module named caffe
How to solve this issue?
Thank you...
You need to add ~/caffe/python to your $PYTHONPATH. either
~$ PYTHONPATH=$PYTHONPATH:~/caffe/python python
>>> import caffe
Or you can do it inside python
>>> import sys
>>> sys.path.insert(0, '~/caffe/python')
>>> import caffe
Alternatively, you can modify your ~/.bashrc file and fix PYTHONPATH environmet variable there

cannot import name module with module correctly installed

new in python.
I have python 2.7.
I just installed a new module (beatifulsoup) from cmd in windows 7 which is correctly installed : When I enter the python consol from the cmd I can import succesfully bs4 without error.
My problem is that I cannot import it when I am using Aptana studio. Or if I code it in sublimetext and save it as a py file, when I run it I get the error message "cannot import name beautifulsoup 4". I don't get why? Is it a problem of pydev environment on aptana? If yes how can I add modules to Aptana and sublimetext3 ?
Are you trying to import like this:
from bs4 import BeautifulSoup
If not, try it that way. Hope this helps!
(Perhaps you should add your import statement to the question so we can see if there is a problem with the statement or if the issue resides somewhere else)

Import error for numpy

I downloaded numpy1.8 for python 2.7
Installed fine, but when i give:
from numpy.fft import fft, fftfreq
it gives me a huge error, which ends with
from numpy.core.multiarray import (
ImportError: cannot import name datetime_data
How can i get rid of this error ?
I need numpy to perform fft functions.
Any kind of help is appreciated.

import error for compat in NLTK and using BrowServer for browsing the NLTK Wordnet database for lemmatization

Extension to the use case here -
NLTK words lemmatizing
I have nltk installed on my computer(with all modules & corpus from the book). My use case is to explore and contrast some lemmatization and stemming approaches for my dataset (I tried Porter lemmatization, which worked)
I was trying to use the lemmatization with Wordnet as described by #Chthonic Project here NLTK words lemmatizing . However the source code it points to(see here http://nltk.org/_modules/nltk/app/wordnet_app.html) , needs compat module from nltk.
from nltk import compat
ImportError: cannot import name compat
I googled around for the import error of compat(and it looked like compatibilty?) and here's what I tried on my ubuntu box:-
sudo find . -name compat* which returns the files below . I also tried sudo find -name "trac" -type d which returns nothing .
I see that I should have found some modules with "trac/tests/functional/fixes" in a likewise folder /usr/lib/python2.4/site-packages/Trac-0.11.1-py2.4.egg/trac/tests/functional/
Source : http://biodegradablegeek.com/2008/08/workaround-for-importerror-cannot-import-name-compat-issue-in-trac-011x/#sthash.NhAThk6e.dpuf
Questions :
1. What am I missing ? And is this an issue with trac/tests?
2. Is there a way to be able to use wordnet for lemmatization (from nltk.corpus import wordnet as wn works just fine. Post the import error is solved, how do I use this module http://nltk.org/_modules/nltk/app/wordnet_app.html (I was trying to build the source locally from this page, i.e. is the file browserver.py, when I hit the import error with compat)
Tip : If you are providing a solution, please also mention how to solve this on my windows environment (I use both windows & ubuntu interchangeably,depending on context)
Files I see from find . -name compat*
ekta#ekta-VirtualBox:/usr/lib/python2.7$ sudo find . -name compat*
./dist-packages/numpy/numarray/compat.pyc
./dist-packages/numpy/numarray/compat.py
./dist-packages/numpy/distutils/compat.pyc
./dist-packages/numpy/distutils/compat.py
./dist-packages/numpy/compat
./dist-packages/numpy/oldnumeric/compat.pyc
./dist-packages/numpy/oldnumeric/compat.py
./dist-packages/twisted/python/compat.pyc
./dist-packages/twisted/python/compat.py
./dist-packages/gtk-2.0/gtk/compat.pyc
./dist-packages/gtk-2.0/gtk/compat.py
I am on python 2.7
Lemmatizing using WordNet (Morphy, actually) in NLTK is simple:
from nltk.corpus import wordnet as wn
wn.morphy('runs') # "run"
wn.morphy('leaves') # "leaf"
wordnet_app is a WordNet browser, not the NLTK WordNet API: you don't need it! Chthonic Project was talking about derivationally related forms, not lemmatizing, which are two different things.
By the way, the issue you had with wordnet_app and compat is that you copied a recent version of the file which was incompatible with your nltk distribution (compat is a recent NLTK module inspired from six that helps the transition to Python 3.). If you need wordnet_app, don't copy the source, simply use the version in your NLTK distribution!)