Not able to Import in NLTK - Python - python

When I run this command in a file or in a shell
import nltk
I get the following error :
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/nltk/__init__.py", line 83, in <module>
from collocations import *
File "/usr/lib/python2.7/dist-packages/nltk/collocations.py", line 37, in <module>
from nltk.probability import FreqDist
File "/usr/lib/python2.7/dist-packages/nltk/probability.py", line 45, in <module>
import random
File "random.py", line 2, in <module>
T = int(raw_input())
ValueError: invalid literal for int() with base 10: ''
Not able to comprehend what is going wrong.

You have a local random module, which masks the random module from standard library.
If you try to import nltk from a different working directory, it should succeed. But in general it's not a good idea name your modules after standard modules, so please rename your random.py file to something else.
You for completeness, let me say that the error was obvious from the last lines of you traceback:
File "random.py", line 2, in <module>
T = int(raw_input())
ValueError: invalid literal for int() with base 10: ''
From the path, random.py, you can tell that the error is in a local file named random.py. And from the exception, you know that something passed an empty string, '', from raw_input to int function, which failed to be converted to int.
Rule of thumb number 2: Always guard you executable code, in a module, in a if __name__ == '__main__': block.

Related

Python3 error when running a pandas simple program

I'm trying to run a python program that uses the pandas library on Mac OS, and I get the next error
adan_vazquez#EPAM-C02G513RML7L automate_python % python3 test_pandas.py
Traceback (most recent call last):
File "/Users/adan_vazquez/Desktop/automate_python/test_pandas.py", line 1, in <module>
import pandas as pd
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/pandas/__init__.py", line 11, in <module>
__import__(dependency)
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/__init__.py", line 155, in <module>
from . import random
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/random/__init__.py", line 180, in <module>
from . import _pickle
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages/numpy/random/_pickle.py", line 1, in <module>
from .mtrand import RandomState
File "mtrand.pyx", line 1, in init numpy.random.mtrand
File "bit_generator.pyx", line 40, in init numpy.random.bit_generator
File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/secrets.py", line 19, in <module>
from random import SystemRandom
File "/Users/adan_vazquez/Desktop/automate_python/random.py", line 3, in <module>
print(random.randint(1,10))
AttributeError: partially initialized module 'random' has no attribute 'randint' (most likely due to a circular import)
Here's the code that I'm trying to run:
import pandas as pd
simpsons = pd.read_html('https://en.wikipedia.org/wiki/List_of_The_Simpsons_episodes_(seasons_1%E2%80%9320)')
print(len(simpsons))
print("Hola")
As you can see is not a big code and I'm getting the error, I already updated my pip3 and my python3 versions, also I reinstall pandas and I keep getting the error, if I try to import pandas in the python terminal I get the same error and I don't know what's causing it, I hope you can help me, thanks in advance.
The error message says it is a circular import error.
Try renaming /Users/adan_vazquez/Desktop/automate_python/random.py with something other than random
According to the error, it is a circular import which is causing the problem.
Since I do not have the details of your project structure or file name, my best guess is that you accidentally named your working file the same as the module name and those modules depend on each other.

ValueError with NERDA model import

I'm trying to import the NERDA library in order use it to engage in a Named-Entity Recognition task in Python. I initially tried importing the library in a jupyter notebook and got the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\oefel\AppData\Local\Programs\Python\Python38\lib\site-packages\NERDA\models.py", line 13, in <module>
from .networks import NERDANetwork
File "C:\Users\oefel\AppData\Local\Programs\Python\Python38\lib\site-packages\NERDA\networks.py", line 4, in <module>
from transformers import AutoConfig
File "C:\Users\oefel\AppData\Local\Programs\Python\Python38\lib\site-packages\transformers\__init__.py", line 43, in <module>
from . import dependency_versions_check
File "C:\Users\oefel\AppData\Local\Programs\Python\Python38\lib\site-packages\transformers\dependency_versions_check.py", line 36, in <module>
from .file_utils import is_tokenizers_available
File "C:\Users\oefel\AppData\Local\Programs\Python\Python38\lib\site-packages\transformers\file_utils.py", line 51, in <module>
from huggingface_hub import HfApi, HfFolder, Repository
File "C:\Users\oefel\AppData\Local\Programs\Python\Python38\lib\site-packages\huggingface_hub\__init__.py", line 31, in <module>
from .file_download import cached_download, hf_hub_url
File "C:\Users\oefel\AppData\Local\Programs\Python\Python38\lib\site-packages\huggingface_hub\file_download.py", line 37, in <module>
if tuple(int(i) for i in _PY_VERSION.split(".")) < (3, 8, 0):
File "C:\Users\oefel\AppData\Local\Programs\Python\Python38\lib\site-packages\huggingface_hub\file_download.py", line 37, in <genexpr>
if tuple(int(i) for i in _PY_VERSION.split(".")) < (3, 8, 0):
ValueError: invalid literal for int() with base 10: '6rc1'
I then tried globally installing using pip in gitbash and got the same error. The library appeared to install without error but when I try the following import, I get that same ValueError:
from NERDA.models import NERDA
I've also tried some of the pre-cooked model imports and gotten the same ValueError.
from NERDA.precooked import EN_ELECTRA_EN
from NERDA.precooked import EN_BERT_ML
I can't find anything on this error online and am hoping someone may be able to lend some insight? Thanks so much!
Take a look at the source code of the used huggingface_hub lib. They comparing the version of your python version to do different imports.
But you uses a release candidate python version (this tells the value '6rc1', that caused the error). Because they didn't expect/handle this, you get the int-parse-ValueError.
Solution 1:
Update your python version to a stable version. No release candidate. So you have an int-only version number.
Solution 2:
Monkeypatch sys.version, before you import the NERDA libs.
sys.version = '3.8.0'

python wants an old version of the library, which I don't have, but I have a new one

I can't program in python at all. I'm just trying to run the grgsm (gnu radio gsm) program, which is written in python. I get following error:
Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/grgsm/__init__.py", line 48, in <module>
from .grgsm_swig import *
File "/usr/lib/python3.8/site-packages/grgsm/grgsm_swig.py", line 13, in <module>
from . import _grgsm_swig
ImportError: libboost_program_options.so.1.71.0: cannot open shared object file: No such file or directory
Traceback (most recent call last):
File "/usr/bin/grgsm_livemon", line 37, in <module>
from grgsm import arfcn
File "/usr/lib/python3.8/site-packages/grgsm/__init__.py", line 48, in <module>
from .grgsm_swig import *
File "/usr/lib/python3.8/site-packages/grgsm/grgsm_swig.py", line 13, in <module>
from . import _grgsm_swig
ImportError: libboost_program_options.so.1.71.0: cannot open shared object file: No such file or directory
From the above message, I concluded that in the 13th line of the file "/usr/lib/python3.8/site-packages/grgsm/grgsm_swig.py" there is an import of the file "libboost_program_options.so.1.71.0", which is missing. Well, but in the 13th line of this file there is nothing about it. It looks like this:
# This file was automatically generated by SWIG (http://www.swig.org).
# Version 4.0.1
#
# Do not make changes to this file unless you know what you are doing--modify
# the SWIG interface file instead.
from sys import version_info as _swig_python_version_info
if _swig_python_version_info < (2, 7, 0):
raise RuntimeError("Python 2.7 or later required")
# Import the low-level C/C++ module
if __package__ or "." in __name__:
from . import _grgsm_swig # 13th line
else:
import _grgsm_swig
try:
import builtins as __builtin__
except ImportError:
import __builtin__
I also don't know why python wants this version of boost. If I knew where it is imported, I would simply change it to libboost_program_options.so without the version suffix (because of course I have boost installed).
Solution can be found here. Generally, uninstall it and reinstall its dependencies in the correct order.

GurobiError: Variable has not yet been added to the model

during typing a code on the python shell for an optimization problem i got this error for unknown reasons GurobiError: Variable has not yet been added to the modeland this is the part of the code:
from gurobipy import *
>>> m=Model("scanning")
>>> from sympy import *
>>> v=zeros(3,1)
>>> ub=zeros(3,1)
>>> lb=zeros(3,1)
>>> x=m.addVar(lb=0,ub=3,vtype='I',name='x')
>>> m.addConstr(Array([[1,2,3],[2,1,0],[2,3,1]])*x>=0,"c0")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/midow/anaconda3/lib/python3.6/site-packages/sympy/tensor/array/ndim_array.py", line 346, in __mul__
other = sympify(other)
File "/Users/midow/anaconda3/lib/python3.6/site-packages/sympy/core/sympify.py", line 266, in sympify
if a in sympy_classes:
File "var.pxi", line 39, in gurobipy.Var.__hash__
gurobipy.GurobiError: Variable has not yet been added to the model
i don't know why am i getting this error despite that syntax is right i guess, i don't know if there's something missing.
i am using Python 3.6.8on macOS high sierra.

Python module import difference "string.maketrans"

I have come across a strange python module import issue.
When I trying to import the boilerpipe module,
from boilerpipe.extract import Extractor
I got this exception:
Original exception was:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/boilerpipe-1.2.0-py2.7.egg/boilerpipe/extract/ __init__.py", line 2, in <module>
import urllib2
File "/usr/lib/python2.7/urllib2.py", line 94, in <module>
import httplib
File "/usr/lib/python2.7/httplib.py", line 1140, in <module>
import ssl
File "/usr/lib/python2.7/ssl.py", line 58, in <module>
import textwrap
File "/usr/lib/python2.7/textwrap.py", line 40, in <module>
class TextWrapper:
File "/usr/lib/python2.7/textwrap.py", line 82, in TextWrapper
whitespace_trans = string.maketrans(_whitespace, ' ' * len(_whitespace))
AttributeError: 'module' object has no attribute 'maketrans'
I've searched over internet and saying that in Python 2.6 the 'str' module has been renamed to 'string' module. So this looks like some where in code library it didn't import "string" module properly.
Yet the really strange thing is, when I run the python code from home directory and run the same piece of code (either by using python shell or using python pyfile.py), it works fine! No more import error.
So I'm bit confusing. Can anyone give me any hint?
Thanks!
Some other script in sys.path is called "string.py" and is masking the stdlib module.
Double check to make sure that you don't have a file string.py that has been imported.
To debug this, put somewhere:
import sys
raise Exception("string module: %r" %(sys.modules.get("string"), ))
That will tell you what string module was imported (or if it shows None, no string module has been imported yet).

Categories