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).
Related
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.
I'm having a problem trying to run a software called pyPENELOPE. After install it perfectly, i've tried run the software and received the error
Traceback (most recent call last):
File "/usr/bin/pypenelope", line 6, in <module>
from penelopetools.gui.main import run
File "/usr/share/python-penelope/penelopetools/gui/main.py", line 52, in <module>
from wxtools.statusbar import EnhancedStatusBar
File "/usr/share/python-penelope/wxtools/statusbar.py", line 72, in <module>
class EnhancedStatusBar(wx.StatusBar):
File "/usr/share/python-penelope/wxtools/statusbar.py", line 74, in EnhancedStatusBar
def __init__(self, parent, id=wx.ID_ANY, style=wx.ST_SIZEGRIP,
AttributeError: 'module' object has no attribute 'ST_SIZEGRIP'
Searching, I found that the problem might be in this call in the code
import wx
and I need to put the lines
import wxversion
wxversion.select('2.8')
import wx
But then i've receive de the message
wxversion.select('2.8')
File "/usr/lib/python2.7/dist-packages/wxversion.py", line 144, in select
raise AlreadyImportedError("wxversion.select() must be called before wxPython is imported")
wxversion.AlreadyImportedError: wxversion.select() must be called before wxPython is imported
Calling wxversion.select('2.8') before the import wxversion i've received
File "/usr/share/python-penelope/wxtools/statusbar.py", line 48, in <module>
wxversion.select('2.8')
NameError: name 'wxversion' is not defined
So.... What can I do?
If, like me, you are using python3 for pyPENELOPE, you can remove the wxversion lines and instead change ST_SIZEGRIP to STB_SIZEGRIP and your code should compile.
Best of luck,
Bunny.
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.
I am trying to import theano in a module, but I am getting a traceback:
File "/media/tarun/6A86CA8286CA4DEF/develop/pydy/pydy/codegen/code.py", line 16, in <module>
import theano
File "/usr/local/lib/python2.7/dist-packages/theano/__init__.py", line 44, in <module>
from theano.gof import \
File "/usr/local/lib/python2.7/dist-packages/theano/gof/__init__.py", line 38, in <module>
from theano.gof.cc import \
File "/usr/local/lib/python2.7/dist-packages/theano/gof/cc.py", line 55, in <module>
StrParam(""))
File "/usr/local/lib/python2.7/dist-packages/theano/configparser.py", line 223, in AddConfigVar
root=newroot, in_c_key=in_c_key)
File "/usr/local/lib/python2.7/dist-packages/theano/configparser.py", line 227, in AddConfigVar
configparam.fullname)
AttributeError: ('This name is already taken', 'gcc.cxxflags')
It seems that there is some name conflict in some config. Can anybody please point me to the same.
This error happens because some module, probably theano.gof, is imported twice. Usually, this is because a first call to import theano.gof gets started, registering 'gcc.cxxflags' in the configuration parser a first time, but then raises ImportError, which is catched and ignored.
Then, import theano.gof gets called again, tries to register the option again, which raises the exception you get.
Is there any traceback or error message before this one, or something that would give a hint of why the first import failed?
I got similar error using when using the jupyter notebook. Restarting kernel solved the issue.
I am trying to make a script that moves all the .txt files in your desktop to desktop/org, the code is as follows:
import os
import shutil
userhome = os.path.expanduser('~')
src = userhome + '/Desktop/'
dst = src+ 'org/'
def main():
txtlist = os.listdir(src)
for file in txtlist:
sortFiles(file)
def sortFiles(file):
if file.endswith(".txt"):
shutil.move(src+file,dst)
main()
If I execute the .py I get this error: AttributeError: 'module' object has no attribute 'copy'. However, if I erase the last line "main()" and I import this script as a module in the python command line and I call .main() from there it works perfectly well. How can I make this work as a script?
Traceback (most recent call last):
File "C:\Python32\org.py", line 3, in <module>
import shutil
File "C:\Python32\lib\shutil.py", line 14, in <module>
import tarfile
File "C:\Python32\lib\tarfile.py", line 50, in <module>
import copy
File "C:\Python32\lib\copy.py", line 61, in <module>
from org.python.core import PyStringMap
File "C:\Python32\org.py", line 19, in <module>
main()
File "C:\Python32\org.py", line 12, in main
sortFiles(file)
File "C:\Python32\org.py", line 16, in sortFiles
shutil.move(src+file,dst)
AttributeError: 'module' object has no attribute 'move'
I am using python 3.2
Wow, that’s some bad luck. You can understand what’s going on when you look at the traceback:
Traceback (most recent call last):
File "C:\Python32\org.py", line 3, in <module>
import shutil
So, the first line that is being executed is import shutil. That’s where everything starts going wrong—which is suprising given that it’s a built-in module.
File "C:\Python32\lib\shutil.py", line 14, in <module>
import tarfile
File "C:\Python32\lib\tarfile.py", line 50, in <module
import copy
So shutil import tarfile, which imports copy.
File "C:\Python32\lib\copy.py", line 61, in <module>
from org.python.core import PyStringMap
And copy has this nice thing that tries to import PyStringMap from a module called org.python.core. Now, this module usually does not exist, which would cause copy to use some alternative code instead: PyStringMap = None.
The problem is, that there is something called org: Your own script, org.py. So what happens is that Python tries to find something called python.core.PyStringMap in your org.py. To be able to go that far, it needs to execute the script, including the main() call at the end:
File "C:\Python32\org.py", line 19, in <module>
main()
File "C:\Python32\org.py", line 12, in main
sortFiles(file)
File "C:\Python32\org.py", line 16, in sortFiles
shutil.copy(src+file,dst)
AttributeError: 'module' object has no attribute 'copy'
And that leads us to the shutil.copy line which is a call to the shutil module. As this is the module we are still importing (from the very first line!), its import hasn’t completely yet, so the copy function inside does not exist, causing the AttributeError.
This is a very unfortunate situation in which the naming of your script caused a circular import for something that doesn’t exist.
You can easily fix this by renaming your script into something else.