NLTK - No module named corpus - python

After installing NLTK and NLTK-DATA with PIP, i run python then i type from nltk.corpus import cmudict and it works.
But when i wrote a script like this:
from nltk.corpus import cmudict
d = cmudict.dict()
def nsyl(word):
return [len(list(y for y in x if y[-1].isdigit())) for x in d[word.lower()]]
print nsyl("hello")
I have the following error :
Traceback (most recent call last):
File "nltk.py", line 1, in <module>
from nltk.corpus import cmudict
File "nltk.py", line 1, in <module>
from nltk.corpus import cmudict
ImportError: No module named corpus
How can i fix this ?
Thanks in advance

From your stacktrace: File "nltk.py", line 1, in <module>, you have called your file nltk.py. When python searches for a module, it looks in the current directory first, and you have "nltk.py" there. It will import this as nltk, and since your code does not define corpus, it can't find nltk.corpus.
To fix this, you should rename your file to something else, say nltkexperience.py. Also make sure to remove "nltk.pyc" from your directory if it exists, since this will also be loaded (it's the byte compiled version of your code). After that, it should work fine.

As others have pointed out, this seems to be a case of version mismatch. If you have multiple versions of Python installed, make sure that the one where you installed NLTK is the one being used when running the script.
As an example, I have Python 2.7, Python 3.3, and Anaconda Python (2.7) installed. My shell defaults to Anaconda (and its pip, e.g.). So when I install something via pip and run it on the command line, it works. At the same time, my Vim is compiled to use the system's Python, and it doesn't see Anaconda's installs/ libraries. So if from within Vim I run Python, I will get an error that the library I installed is not found.
Hope this helps.

Related

Python third party Module global import

i'm currently into learning a bit of python and i want to import the paperclip third party module into my python file.
Yes, i already installed the pyperclip module with
pip install pyperclip.
if i create a file on my desktop, i get an error which says
Traceback (most recent call last):
File "test.py", line 1, in <module>
import pyperclip
ImportError: No module named pyperclip
However if i put the test.py in my python folder, it runs.
The question now is, is there a way to make all my installed modules available on a global scope ? I just want to have my file e.g. on my Desktop and run it without having import issues.
Thank you.
Greetings
Edit: I'm working on a Mac, maybe this leads to the problem
Found the problem.
The pip installautomatically used pip3.5 install
whereas python test.pydidn't use python3.5 test.py
Thank you #Bakurìu
Is there a way i can define python3.5as python?
You can do this :
pip3.5 install paperclip
pyperclip is install but not paperclip

Import error with spacy: "No module named en"

I'm having trouble using the Python spaCy library. It seems to be installed correctly but at
from spacy.en import English
I get the following import error:
Traceback (most recent call last):
File "spacy.py", line 1, in <module>
from spacy.en import English
File "/home/user/CmdData/spacy.py", line 1, in <module>
from spacy.en import English
ImportError: No module named en
I'm not very familiar with Python but that's the standard import I saw online, and the library is installed:
$ pip list | grep spacy
spacy (0.99)
EDIT
I tested renaming the file, but that's not the problem. I also get the same error when doing:
$ python -m spacy.en.download --force all
/usr/bin/python: No module named en
(The command is supposed to download some models)
For windows, open cmd with admin right. Then,
python -m spacy download en
You should see the shell prompt stating.
You can now load the model via spacy.load('en')
You are facing this error because you named your own file spacy.py. Rename your file, and everything should work.
I had the same issue, and the problem was the folder where the module 'en' was stored (spacy/lang/en).
Typing:
from spacy.lang.en import English
fixed the issue.
This post was helpful in figuring this out.
It is possible that the version of Python at /usr/bin/python is not the one that has spacy installed. If so, navigating to the directory where your 'normal' version of Python is before running
python -m spacy.en.download
should fix the problem. (For example, I installed spacy using Anaconda and had to navigate to C:\Anaconda2\ first.)
SpaCy has various models depending on the language of your choice (even contains a multi-language model), so you can have a look at this link to have a better idea on which might suit your needs.
You could also find the correct installation command here. For example, for small version model for English Language:
python -m spacy download en_core_web_sm
Hope it helps!
This Works!
import spacy
import en_core_web_sm
nlp = en_core_web_sm.load()

Raspberry Pi Python Program First Line Error/Python Library Error

The first two lines of the program are:
from i2clibraries import i2c_lcd
from ABElectronics_ADCPi import ADCPi
No matter what line is first the Pi returns an error when I attempt to run it under Python or Python 3. All the libraries are possessed and registered. Using the shell commands the checks saying the exports worked correctly all show up correctly. However, whatever line is line 1 will return a missing module error and the i2clibraries will always return a missing module error. By keeping that as the first line I get the least errors in running, but the program still returns this:
pi#raspberrypi ~ $ sudo python file.py
Traceback (most recent call last):
File "file.py", line 1, in <module>
from i2clibraries import i2c_lcd
File "/home/pi/i2clibraries/i2c_lcd.py", line 1, in <module>
from i2clibraries import i2c
File "/home/pi/i2clibraries/i2c.py", line 1, in <module>
from quick2wire.i2c import I2CMaster, writing_bytes, reading
ImportError: No module named quick2wire.i2c
Given the error, what possible solutions are there to stop the first line from being unable to find its module?
Problem
The error message is telling you that when you try to import the i2clibraries module, the imports that it requires (dependencies) cannot be found when it is itself is being imported. This is specifically in the first line of i2c.py file - where the line
from quick2wire.i2c import I2CMaster, writing_bytes, reading
is failing.
The problem is almost certainly that your modules are not on the Python module search path. Further info on this is given at the end of this answer should you need it.
Solution
There are a number of ways to resolve this. The one recommended by the developers of the module is
To use the library without installation, add the full path of the
source tree to the PYTHONPATH environment variable. For example:
export QUICK2WIRE_API_HOME=[the directory cloned from Git or unpacked from the source archive]
export PYTHONPATH=$PYTHONPATH:$QUICK2WIRE_API_HOME
So, you need to know where your quick2wire libraries are installed - from your error message, I would hazard a guess that they are in /home/pi/i2clibraries/, so $QUICK2WIRE_API_HOME=/home/pi/i2clibraries/ should be your first line of the above pair.
Further info
You can read more generally about how to install modules on Python 2.x on the Python website. You can look at what paths make up the module search path by going to an interactive Python prompt (i.e. typing python) and then doing.
>>> import sys
>>> sys.path
This will output a list containing strings representing all the paths that will be searched for modules.

Error during library import in Python (Windows)

I am trying to configure svn hook for email notification using mailer.py script from apache, but it always crash on import of svn libs. I try two ways of installing svn and python. I try to install everything manually using command line and use CollableNet installer that have preinstalled Python and all libs I need but result is the same.
Now I try to import one library from hole package that I need using python in command line and I have such response:
>>> import svn.core
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named svn.core
I try to fix it using:
1) import sys; sys.path.append("C:\csvn\lib\svn-python\svn")
and
2) C:\csvn\lib\svn-python\svn>python "\__init__.py" install
But it didn't help.
For now I have no package with setup.py to install it.
I spend a lot of time on this task, and going crazy a little). Please give me any suggestion how to fix it.

Importing pyFileMaker complains about EXPAT library

I've just downloaded pyFileMaker. I copied the directory PyFileMaker into the dir Lib under Python31 directory, but when I simply include the module FMServer with this line:
from PyFileMaker import FMServer
I get this error
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
from PyFileMaker import FMServer
File "C:\Python31\lib\PyFileMaker\__init__.py", line 17
print "Unable to load the EXPAT library. You need to have it installed"
I checked whether the module expat exists and, infact, it exists. How can I fix this?
Well, as far as I can see the error happened not in the import, but in the print statement. (I.e. there probably was an error in import too, but it was intercepted.) The problem is that PyFileMaker is written for Python 2.x (their site says 2.4 is the minimal version), while you're using Python 3, and v3 is not compatible with v2.x.
I'd suggest switching to Python v2.7 (which is the primary version at the moment anyway). If you really want to use v3, you might try to convert the file with the 2to3 converter, but there's no guarantee it's going to work.

Categories

Resources