Why can't I import nltk? - python

In windows with a 64 bit OS, once I installed nltk-2.0.4.win-amd64-py2.7.exe,from http://www.lfd.uci.edu/~gohlke/pythonlibs/, for my 64 bit python, I ran 'import nltk' to get the following error!
Traceback (most recent call last):
File "<pyshell#25>", line 1, in <module>
import nltk
File "C:\Python27\lib\site-packages\nltk\__init__.py", line 131, in <module>
from inference import *
File "C:\Python27\lib\site-packages\nltk\inference\__init__.py", line 19, in <module>
from discourse import (ReadingCommand, CfgReadingCommand,
File "C:\Python27\lib\site-packages\nltk\inference\discourse.py", line 54, in <module>
from nltk.sem.glue import DrtGlue
File "C:\Python27\lib\site-packages\nltk\sem\glue.py", line 13, in <module>
from nltk.corpus import brown
File "C:\Python27\lib\site-packages\nltk\corpus\__init__.py", line 70, in <module>
from reader import *
File "C:\Python27\lib\site-packages\nltk\corpus\reader\__init__.py", line 85, in <module>
from nltk.corpus.reader.pl196x import *
File "C:\Python27\lib\site-packages\nltk\corpus\reader\pl196x.py", line 11, in <module>
from nltk import tokenize, tree
ImportError: cannot import name tokenize
Suggestions on how to fix this would be greatly appreciated. Thanks

I solve this problem as following reference link. It seems that the Python installer sometimes can't create proper registry entries in Win7 environment, users need to create them manually.
Ref link:http://www.fbagirov.com/2012/10/13/installing-nltk-for-python/

I solved this by creating a environment variable "PYTHONPATH" with the value of my python installation path.

Related

cvxpy import error : cannot read file data

I'm trying to use cvxpy on a pythonanywhere server, the installation was ok, no errors however when i want to import cvpxy as cp, i have an error, help me please
the error :
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/padacore/.virtualenvs/env/lib/python3.7/site-packages/cvxpy/__init__.py", line 18, in <module>
from cvxpy.atoms import *
File "/home/padacore/.virtualenvs/env/lib/python3.7/site-packages/cvxpy/atoms/__init__.py", line 17, in <module>
from cvxpy.atoms.cummax import cummax
File "/home/padacore/.virtualenvs/env/lib/python3.7/site-packages/cvxpy/atoms/cummax.py", line 17, in <module>
from cvxpy.atoms.atom import Atom
File "/home/padacore/.virtualenvs/env/lib/python3.7/site-packages/cvxpy/atoms/atom.py", line 21, in <module>
from cvxpy.expressions.constants import Constant
File "/home/padacore/.virtualenvs/env/lib/python3.7/site-packages/cvxpy/expressions/constants/__init__.py", line 17, in <module>
from .constant import Constant
File "/home/padacore/.virtualenvs/env/lib/python3.7/site-packages/cvxpy/expressions/constants/constant.py", line 22, in <module>
from scipy.sparse.linalg import eigsh
File "/home/padacore/.virtualenvs/env/lib/python3.7/site-packages/scipy/sparse/linalg/__init__.py", line 111, in <module>
from .isolve import *
File "/home/padacore/.virtualenvs/env/lib/python3.7/site-packages/scipy/sparse/linalg/isolve/__init__.py", line 4, in <module>
from .iterative import *
File "/home/padacore/.virtualenvs/env/lib/python3.7/site-packages/scipy/sparse/linalg/isolve/iterative.py", line 8, in <module>
from . import _iterative
ImportError: /home/padacore/.virtualenvs/env/lib/python3.7/site-packages/scipy/sparse/linalg/isolve/../../../../scipy.libs/libopenblasp-r0-085ca80a.3.9.so: cannot read file data
It could be something to do with Atom, try switching to another editor. I recommend PyCharm or VSCode. If you're choosing VSCode, install these extensions: Python, PyLance, and Jupyter.
If you don't like VSCode's default themes, and miss Atom's One Dark, then there's an extension just for that! Just search for it, and you'll find it!
EDIT: If it wasn't something to do with Atom, maybe install an older version of cvxpy

ImportError: DLL load failed: The file cannot be accessed by the system

Traceback (most recent call last):
File "<ipython-input-9-a40793f9ded7>", line 7, in <module>
from sklearn.model_selection import train_test_split
File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\__init__.py", line 76, in <module>
from .base import clone
File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\base.py", line 16, in <module>
from .utils import _IS_32BIT
File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\__init__.py", line 20, in <module>
from .validation import (as_float_array,
File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\validation.py", line 21, in <module>
from .fixes import _object_dtype_isnan
File "C:\ProgramData\Anaconda3\lib\site-packages\sklearn\utils\fixes.py", line 18, in <module>
from scipy.sparse.linalg import lsqr as sparse_lsqr # noqa
File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\sparse\linalg\__init__.py", line 113, in <module>
from .isolve import *
File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\sparse\linalg\isolve\__init__.py", line 6, in <module>
from .iterative import *
File "C:\ProgramData\Anaconda3\lib\site-packages\scipy\sparse\linalg\isolve\iterative.py", line 10, in <module>
from . import _iterative
ImportError: DLL load failed: The file cannot be accessed by the system.
Got this error while trying to run some already used python scripts on pycharm. Has anybody had this error before and has he solved it? It started after I installed a package in python but never used it. The problem is located when I try to import train_test_split on scikit-learn and tensorflow, the sklearn package and seaborn. The other parts of the code seem unaffected.
I have already reinstalled anaconda and python as well as changing the python files. Any help would be greatly appreciated.
After reinstalling the python as well as updating all the packages (conda update --all) and changing the PATH file, it seems that the issue is solved in all of my IDE's except PyCharm. It seems that all that remains is an interpreter issue. I will post updates if something else comes into my attention.

Python embedding, ImportError: cannot import name _remove_dead_weakref

I am trying to run some C++ code with embedded Python. Code was working completely fine a couple of weeks ago. The code still compiles, but I am now getting the error
Traceback (most recent call last):
File "./main.py", line 1, in <module>
import numpy as np
File "/usr/local/lib/python2.7/site-packages/numpy/__init__.py", line 142, in <module>
from . import add_newdocs
File "/usr/local/lib/python2.7/site-packages/numpy/add_newdocs.py", line 13, in <module>
from numpy.lib import add_newdoc
File "/usr/local/lib/python2.7/site-packages/numpy/lib/__init__.py", line 8, in <module>
from .type_check import *
File "/usr/local/lib/python2.7/site-packages/numpy/lib/type_check.py", line 11, in <module>
import numpy.core.numeric as _nx
File "/usr/local/lib/python2.7/site-packages/numpy/core/__init__.py", line 74, in <module>
from numpy.testing.nosetester import _numpy_tester
File "/usr/local/lib/python2.7/site-packages/numpy/testing/__init__.py", line 10, in <module>
from unittest import TestCase
File "/usr/local/opt/python/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/__init__.py", line 64, in <module>
from .main import TestProgram, main
File "/usr/local/opt/python/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/main.py", line 7, in <module>
from . import loader, runner
File "/usr/local/opt/python/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/runner.py", line 7, in <module>
from .signals import registerResult
File "/usr/local/opt/python/Frameworks/Python.framework/Versions/2.7/lib/python2.7/unittest/signals.py", line 2, in <module>
import weakref
File "/usr/local/opt/python/Frameworks/Python.framework/Versions/2.7/lib/python2.7/weakref.py", line 14, in <module>
from _weakref import (
ImportError: cannot import name _remove_dead_weakref
Since then the only change I have made is to install the macOS Supplemental Update and to upgrade Python from 2.7.13_1 to 2.7.14_2 (using Homebrew). I have changed the relevant linker flags in the makefile, i.e. those that appear when I type python-config --ldflags. I have also made sure that the Python I am using is definitely the Homebrew one by inserting export PATH="/usr/local/opt/python/libexec/bin:$PATH" into my .bash_profile as directed by Homebrew.
I am not sure whether this error is because of the Python update, the recent macOS Supplemental Update, or something else. Any idea what the problem could be?
Thanks
I had the same problem;
In python 2.7 the NumPy 1.8.0 was available,
Then I installed NumPy (again) using brew, and it wasn't available in my python (2.7) anymore. (Got the same error you got here)
So I did "brew unlink python && brew link python" to use python 3 (instead of 2) which NumPy 1.14 is available in it.
I guess the python 2.7 was not compatible with this recent version of NumPy.

how to fix ImportError: cannot import name 'format_exception_only' in python

I am using pycharm community version for python 3.6.1 as I'm beginner when i try to run simple tokenize program -
import nltk
from nltk.tokenize import word_tokenize
word_tokenize(" hellow world im nltk")
I get the following error -
C:\Users\Amnaa\AppData\Local\Programs\Python\Python36-32\python.exe E:/test/token.py
Traceback (most recent call last):
File "E:/test/token.py", line 1, in <module>
import nltk
File "C:\Users\Amnaa\AppData\Local\Programs\Python\Python36-32\lib\site-packages\nltk\__init__.py", line 89, in <module>
from nltk.internals import config_java
File "C:\Users\Amnaa\AppData\Local\Programs\Python\Python36-32\lib\site-packages\nltk\internals.py", line 11, in <module>
import subprocess
File "C:\Users\Amnaa\AppData\Local\Programs\Python\Python36-32\lib\subprocess.py", line 126, in <module>
import threading
File "C:\Users\Amnaa\AppData\Local\Programs\Python\Python36-32\lib\threading.py", line 7, in <module>
from traceback import format_exc as _format_exc
File "C:\Users\Amnaa\AppData\Local\Programs\Python\Python36-32\lib\traceback.py", line 5, in <module>
import linecache
File "C:\Users\Amnaa\AppData\Local\Programs\Python\Python36-32\lib\linecache.py", line 11, in <module>
import tokenize
File "C:\Users\Amnaa\AppData\Local\Programs\Python\Python36-32\lib\tokenize.py", line 35, in <module>
from token import *
File "E:\test\token.py", line 2, in <module>
from nltk.tokenize import word_tokenize
File "C:\Users\Amnaa\AppData\Local\Programs\Python\Python36-32\lib\site-packages\nltk\tokenize\__init__.py", line 64, in <module>
from nltk.tokenize.mwe import MWETokenizer
File "C:\Users\Amnaa\AppData\Local\Programs\Python\Python36-32\lib\site-packages\nltk\tokenize\mwe.py", line 31, in <module>
from nltk.util import Trie
File "C:\Users\Amnaa\AppData\Local\Programs\Python\Python36-32\lib\site-packages\nltk\util.py", line 13, in <module>
import pydoc
File "C:\Users\Amnaa\AppData\Local\Programs\Python\Python36-32\lib\pydoc.py", line 72, in <module>
from traceback import format_exception_only
ImportError: cannot import name 'format_exception_only'
Process finished with exit code 1
File "E:\test\token.py", line 2, in
from nltk.tokenize import word_tokenize
There happen to be another script with the same name Python is looking for. Python got confused about the script from package and your own script because it start searching from the folder where you are.
So just change your own script to another name.
Go to command prompt(not necessarily command prompt. You can do it from IDLE also). Do an nltk.download() in the python interpreter and then download all-corpora and book. After you do this, run your program. It should work.

Random errors in Anaconda3

C:\Users\jesper>pip Traceback (most recent call last): File
"D:\Anaconda3\Scripts\pip-script.py", line 3, in <module>
import pip File "D:\Anaconda3\lib\site-packages\pip\__init__.py", line 14, in <module>
from pip.utils import get_installed_distributions, get_prog File "D:\Anaconda3\lib\site-packages\pip\utils\__init__.py", line 22, in
<module>
from pip.compat import console_to_str, expanduser, stdlib_pkgs File "D:\Anaconda3\lib\site-packages\pip\compat\__init__.py", line 11,
in <module>
from logging.config import dictConfig as logging_dictConfig File "D:\Anaconda3\lib\logging\config.py", line 30, in <module>
import logging.handlers File "D:\Anaconda3\lib\logging\handlers.py", line 26, in <module>
import logging, socket, os, pickle, struct, time, re File "D:\Anaconda3\lib\socket.py", line 123, in <module>
class socket(_socket.socket): TypeError: __weakref__ slot disallowed: either we already got one, or __itemsize__ != 0
I get this now, and it happened randomly and keeps recurring. I can't use Python at the moment. I tried re-installing the entire conda library and tried different locations for Anaconda. Even tried installing Anaconda for 2.7 and it still didn't work. If someone knows how to fix I'd be in there eternal debt. Thanks!

Categories

Resources