ImportError in a simple NLTK example - python

I'm new with Python and NLTK
When I test the following lines in the Python console
import nltk.data
tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
text ="toto. titi. tutu"
tokens = tokenizer.tokenize(text)
print(tokens)
I get what I expect. But when I execute these lines from a file, for example with the command line > python tokenize.py, I get errors:
C:\outils\Python\WinPython-64bit-3.4.3.5\python-3.4.3.amd64\python.exe C:/Documents/Dvpt/SemanticAndOpenData/scholar/scholar.py/tokenize.py
Traceback (most recent call last):
File "C:/Documents/Dvpt/SemanticAndOpenData/scholar/scholar.py/tokenize.py", line 1, in <module>
import nltk.data
File "C:\outils\Python\WinPython-64bit-3.4.3.5\python-3.4.3.amd64\lib\site-packages\nltk\__init__.py", line 89, in <module>
from nltk.internals import config_java
File "C:\outils\Python\WinPython-64bit-3.4.3.5\python-3.4.3.amd64\lib\site-packages\nltk\internals.py", line 11, in <module>
import subprocess
File "C:\outils\Python\WinPython-64bit-3.4.3.5\python-3.4.3.amd64\lib\subprocess.py", line 395, in <module>
import threading
File "C:\outils\Python\WinPython-64bit-3.4.3.5\python-3.4.3.amd64\lib\threading.py", line 10, in <module>
from traceback import format_exc as _format_exc
File "C:\outils\Python\WinPython-64bit-3.4.3.5\python-3.4.3.amd64\lib\traceback.py", line 3, in <module>
import linecache
File "C:\outils\Python\WinPython-64bit-3.4.3.5\python-3.4.3.amd64\lib\linecache.py", line 10, in <module>
import tokenize
File "C:\Documents\Dvpt\SemanticAndOpenData\scholar\scholar.py\tokenize.py", line 2, in <module>
tokenizer = nltk.data.load('tokenizers/punkt/english.pickle')
File "C:\outils\Python\WinPython-64bit-3.4.3.5\python-3.4.3.amd64\lib\site-packages\nltk\data.py", line 786, in load
resource_val = pickle.load(opened_resource)
File "C:\outils\Python\WinPython-64bit-3.4.3.5\python-3.4.3.amd64\lib\site-packages\nltk\tokenize\__init__.py", line 63, in <module>
from nltk.tokenize.simple import (SpaceTokenizer, TabTokenizer, LineTokenizer,
File "C:\outils\Python\WinPython-64bit-3.4.3.5\python-3.4.3.amd64\lib\site-packages\nltk\tokenize\simple.py", line 38, in <module>
from nltk.tokenize.api import TokenizerI, StringTokenizer
File "C:\outils\Python\WinPython-64bit-3.4.3.5\python-3.4.3.amd64\lib\site-packages\nltk\tokenize\api.py", line 13, in <module>
from nltk.internals import overridden
ImportError: cannot import name 'overridden'
Process finished with exit code 1
And I'm stuck on the problem and I can't find a way to solve it. Thanks in advance for any useful proposal.

You need to name the script something other than tokenize.py

The problem here is that you have named your script as tokenize.py. Try renaming the file to something like my_tokenizer.py. Actually what is happening is that when you are using
import tokenize
What it is doing is trying to import the current file itself and thus you are getting the errors.

Related

What is this error code telling me? i.e. what do I need to fix

What does this error code mean? (see first answer for error code) I got this while attempting to open a csv file and read the first 5 rows of data.
C:\Users\torou>python cleandep/py
python: can't open file 'cleandep/py': [Errno 2] No such file or directory
C:\Users\torou>python http://cleandep.py
Traceback (most recent call last):
File "bit_generator.pyx", line 40, in numpy.random.bit_generator
File "C:\Users\torou\AppData\Local\Programs\Python\Python37-2\lib\http://secrets.py", line 20, in <module>
from random import SystemRandom
File "C:\Users\torou\http://random.py", line 1, in <module>
from random import random
ImportError: cannot import name 'random' from 'random' C:\Users\torou\http://random.py)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "http://cleandep.py", line 8, in <module>
import numpy as npy
File "C:\Users\torou\AppData\Local\Programs\Python\Python37-32\lib\site-packages\numpy\__init__.py", line 150, in <module>
from . import random
File "C:\Users\torou\AppData\Local\Programs\Python\Python37-32\lib\site-packages\numpy\random\__init__.py", line 181, in <module>
from . import _pickle
File "C:\Users\torou\AppData\Local\Programs\Python\Python37-32\lib\site-packages\numpy\random\_pickle.py", line 1, in <module>
from .mtrand import RandomState
File "mtrand.pyx", line 9, in init numpy.random.mtrand
File "mt19937.pyx", line 1, in init numpy.random.mt19937
File "bit_generator.pyx", line 43, in init numpy.random.bit_generator
File "C:\Users\torou\http://random.py", line 1, in <module>
from random import random
ImportError: cannot import name 'random' from 'random' C:\Users\torou\http://random.py)
This is the python 3 code i attempted to run:
import numpy as npy
import pandas as pd
data= pd.read_csv("cti-april02-log")
data.head()
you have a file named random.py that override the module with the same name from the standard library (i.e. it try to import random from your file random.py, not the module in the standard library

when I run this code it shows error

filename='metamorphosis_clean.txt'
file=open(filename,'rt')
text=file.read()
file.close()
from nltk import sent_tokenize
sentences=sent_tokenize(text)
print(sentences[0])
Error:
Traceback (most recent call last):
File "split_into_sentenes.py", line 1, in <module>
import nltk
File "/usr/local/lib/python2.7/dist-packages/nltk/__init__.py", line 114, in <module>
from nltk.collocations import *
File "/usr/local/lib/python2.7/dist-packages/nltk/collocations.py", line 37, in <module>
from nltk.probability import FreqDist
File "/usr/local/lib/python2.7/dist-packages/nltk/probability.py", line 47, in <module>
from collections import defaultdict, Counter
File "/usr/local/lib/python2.7/dist-packages/nltk/collections.py", line 13, in <module>
import pydoc
File "/usr/lib/python2.7/pydoc.py", line 56, in <module>
import sys, imp, os, re, types, inspect, __builtin__, pkgutil, warnings
File "/usr/lib/python2.7/inspect.py", line 39, in <module>
import tokenize
File "/usr/lib/python2.7/tokenize.py", line 39, in <module>
COMMENT = N_TOKENS
NameError: name 'N_TOKENS' is not defined
In all likelihood you have a file named token.py in the current directory, i.e. the directory from which you are running your split_into_sentenes.py script.
If present locally, token.py will be imported before the one in the standard library, and this would result in the error that you see.
Check whether it exists and if necessary rename it to something that doesn't clash with the standard library.

Mac system generate error message when I call python script on terminal

My code is simply the follow and work few days ago:
import pandas as pd
df = pd.read_csv('WORLDBANK-ZAF_MYS_PROP_4044_SEC_MF.csv')
print(df.head())
But now whenever I try to run it by calling python my_io.py on my Mac terminal it generates the following messages:
Bases-MacBook-Pro:data_analysis me$ python my_io.py
Traceback (most recent call last):
File "my_io.py", line 1, in
import pandas as pd
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/init.py", line 13, in
import(dependency)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/init.py", line 142, in
from . import add_newdocs
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/add_newdocs.py", line 13, in
from numpy.lib import add_newdoc
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/init.py", line 8, in
from .type_check import *
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/type_check.py", line 11, in
import numpy.core.numeric as _nx
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/init.py", line 72, in
from numpy.testing.nosetester import _numpy_tester
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/testing/init.py", line 12, in
from . import decorators as dec
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/testing/decorators.py", line 20, in
from .utils import SkipTest, assert_warns
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/testing/utils.py", line 15, in
from tempfile import mkdtemp, mkstemp
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tempfile.py", line 32, in
import io as _io
File "/Users/gongzhuli/Desktop/data_analysis/io.py", line 3, in
AttributeError: 'module' object has no attribute 'read_csv'
Can someone please help me, I have no idea what is going on here..
Your file is called io.py, which is a library module to handle file-like buffers among other things.
pandas imports tempfile which needs it:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/tempfile.py", line 32, in <module>
import io as _io
and your filename gets in the way of the import chain and prevents import from going through.
Just rename your file with something more specific (like my_io.py for instance?)

Weird behaviour with tweepy in Python?

I have python 2.7.9 on OS X (10.11). I have installed tweepy 3.5.0 and simplejson 3.8.1. When i write a program (IDE: textWrangler) for updating status of twitter account (by using tweepy) and run the program using command line it throws error on "import tweepy" in a program. However when i run the same code on python shell, it execute sucessfuly (without any error). Any suggestions or comments?
Traceback (most recent call last):
File "tweepytesting.py", line 1, in <module>
import tweepy
File "build/bdist.macosx-10.6-intel/egg/tweepy/__init__.py", line 14, in <module>
File "build/bdist.macosx-10.6-intel/egg/tweepy/api.py", line 12, in <module>
File "build/bdist.macosx-10.6-intel/egg/tweepy/binder.py", line 11, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/__init__.py", line 53, in <module>
from .packages.urllib3.contrib import pyopenssl
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/packages/__init__.py", line 27, in <module>
from . import urllib3
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/packages/urllib3/__init__.py", line 8, in <module>
from .connectionpool import (
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 33, in <module>
from .packages.ssl_match_hostname import CertificateError
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/packages/urllib3/packages/__init__.py", line 3, in <module>
from . import ssl_match_hostname
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests/packages/urllib3/packages/ssl_match_hostname/__init__.py", line 3, in <module>
from ssl import CertificateError, match_hostname
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 90, in <module>
import textwrap
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/textwrap.py", line 40, in <module>
class TextWrapper:
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/textwrap.py", line 82, in TextWrapper
whitespace_trans = string.maketrans(_whitespace, ' ' * len(_whitespace))
AttributeError: 'module' object has no attribute 'maketrans'
If I add this line:
import string; print(string)
just above import tweepy, then the following is printed:
<module 'string' from '/Users/yasirmuhammad/Desktop/learnpython/string.pyc'>

Cannot import name mlab

I've encountered a very strange error when trying to Import mayavi. A few minutes ago it worked like a charm, but now there's something going wrong and I've no clue why.
I start python from the terminal and then I type:
from mayavi import mlab
The error I get:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/mayavi/mlab.py", line 27, in <module>
from mayavi.tools.camera import view, roll, yaw, pitch, move
File "/usr/lib/python2.7/dist-packages/mayavi/tools/camera.py", line 23, in <module>
from engine_manager import get_engine
File "/usr/lib/python2.7/dist-packages/mayavi/tools/engine_manager.py", line 14, in <module>
from mayavi.core.engine import Engine
File "/usr/lib/python2.7/dist-packages/mayavi/core/engine.py", line 28, in <module>
from mayavi.core.scene import Scene
File "/usr/lib/python2.7/dist-packages/mayavi/core/scene.py", line 15, in <module>
from mayavi.core.source import Source
File "/usr/lib/python2.7/dist-packages/mayavi/core/source.py", line 19, in <module>
from mayavi.core.module_manager import ModuleManager
File "/usr/lib/python2.7/dist-packages/mayavi/core/module_manager.py", line 19, in <module>
from mayavi.core.lut_manager import LUTManager
File "/usr/lib/python2.7/dist-packages/mayavi/core/lut_manager.py", line 10, in <module>
import subprocess
File "/usr/lib/python2.7/subprocess.py", line 432, in <module>
import pickle
File "pickle.py", line 4, in <module>
from mayavi import mlab
ImportError: cannot import name mlab
What's wrong with Python?
You have a local file named pickle.py; this is being imported instead of the pickle module. This module then tries to import mlab before that module itself has completed importing in a circular import dependency:
File "/usr/lib/python2.7/subprocess.py", line 432, in <module>
import pickle
File "pickle.py", line 4, in <module>
from mayavi import mlab
Note how subprocess tries to import pickle and finds your file instead (the path is relative instead of a full path inside /usr/lib/python2.7/.
Rename pickle.py to something else, you are masking the standard library here.

Categories

Resources