AttributeError: 'module' object has no attribute 'Template' - python

I'm using MacOs with the PyCharm environment, and my project is set to use Python 2.7.10. Now, I have on the first line of my module the line:
import numpy as np
When I try to run my code, I get the following error:
/System/Library/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /{path to file}/numpy_exercise.py
Traceback (most recent call last):
File "/{path to file}/numpy_exercise.py", line 1, in module>
import numpy as np
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/__init__.py", line 169, in module>
from . import polynomial
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/polynomial/__init__.py", line 20, in module>
from .polynomial import Polynomial
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/polynomial/polynomial.py", line 68, in module>
from .polytemplate import polytemplate
File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/polynomial/polytemplate.py", line 17, in module>
polytemplate = string.Template(''' AttributeError: 'module' object has no attribute 'Template'
(note that "module>" was actually in brackets from both sides but because it gets omitted that way I daleted the first bracket so you can see it)
Though in Python 3 it does not happens to me. I looked on the web but couldn't find anything. I have no clue of why this is happening. I'd be glad to get some of your help!
Thank you guys!

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.

AttributeError: 'module' object has no attribute 'ST_SIZEGRIP'

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.

My python code runs once, but I get an error when I run it twice

I have a little python script that checks my current IPv4 DNS Server address. It's working just fine. I use the wmi package.
Now: I use this script in Kodi. And first time it runs, it runs perfectly. But the second time I get a weird error.
Why does my code work the first time, but not the following attempts?
Hope you guys can help me. I'm lost.
my script:
import wmi
nic_configs = wmi.WMI().Win32_NetworkAdapterConfiguration(IPEnabled=True);
nic = nic_configs[0];
dns = nic.DNSServerSearchOrder[0];
the error:
Error Type: <type 'exceptions.AttributeError'>
Error Contents: 'NoneType' object has no attribute 'platform'
Traceback (most recent call last):
File "C:\Python\Lib\site-packages\mytest.py", line 22, in <module>
import wmi
File "C:\Python\Lib\site-packages\wmi.py", line 88, in <module>
from win32com.client import GetObject, Dispatch
File "C:\Python\Lib\site-packages\win32com\__init__.py", line 6, in <module>
import pythoncom
File "C:\Python\Lib\site-packages\pythoncom.py", line 3, in <module>
pywintypes.__import_pywin32_system_module__("pythoncom", globals())
File "C:\Python\Lib\site-packages\pywintypes.py", line 20, in __import_pywin32_system_module__
if not sys.platform.startswith("win32"):
AttributeError: 'NoneType' object has no attribute 'platform'
-->End of Python script error report<--

Problems importing grass.script into PyDev

I am trying to do grass scripting in Eclipse and i follow the instructions outlined in this page. I think i have everything configured however when i try to import grass.script i get the following message:
Traceback (most recent call last):
File "/home/nesic/Desktop/grass_dev/Simulacije/test.py", line 6, in <module>
import grass.script as grass
File "/usr/lib/grass64/etc/python/grass/script/__init__.py", line 1, in <module>
from core import *
File "/usr/lib/grass64/etc/python/grass/script/core.py", line 38, in <module>
gettext.install('grasslibs', os.path.join(os.getenv("GISBASE"), 'locale'), unicode=True)
File "/usr/local/lib/python2.6/posixpath.py", line 67, in join
elif path == '' or path.endswith('/'):
AttributeError: 'NoneType' object has no attribute 'endswith'
What am I doing wrong?
os.getenv("GISBASE") most likely returns None, because the envorinment variable GISBASE is not set. That then leads to the AttributeError: 'NoneType' object has no attribute 'endswith' you're seeing.
Try setting GISBASE according to http://grass.osgeo.org/grass70/manuals/variables.html:
GISBASE
directory where GRASS lives. This is set automatically by the startup script.
(BTW, if you don't know about it already, there's also gis.stackexchange.org)

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