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.
Related
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'm new to Python and I'm trying to do a simple thread as follows.
import threading
def func(x):
print x
t1 = threading.Thread(target=func,args=("Hello",));
t1.start();
Then I got following error:
Traceback (most recent call last):
File "ex2.py", line 1, in <module>
import threading
File "/Users/treinetic-macbook/Desktop/threading.py", line 2, in <module>
File "/Library/Python/2.7/site-packages/requests/__init__.py", line 43, in <module>
import urllib3
File "/Library/Python/2.7/site-packages/urllib3/__init__.py", line 8, in <module>
from .connectionpool import (
File "/Library/Python/2.7/site-packages/urllib3/connectionpool.py", line 3, in <module>
import logging
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/logging/__init__.py", line 207, in <module>
_lock = threading.RLock()
AttributeError: 'module' object has no attribute 'RLock'
can someone help me to understand this error?
/Users/treinetic-macbook/Desktop/threading.py is being imported because it's on your path somewhere. This is most likely not correct, try renaming that file and removing any threading.pyc file in your local dir.
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).
I am new to python.And i am learning the standard library.
Whenever i run the code below , it always raise the AttributeError...
And it seems like there is something wrong with the import command.
Also , i try to run it on the interactive interpreator,and it works just fine.
The sample code
import tempfile
import os
#temp = tempfile.TemporaryFile()
temp = tempfile.mktemp()
print "tempfile","=>",temp
file = open(temp,"w+b")
file.write("*" * 1000)
file.seek(0)
print len(file.read()),"byte"
file.close()
try:
os.remove(temp)
except OSError:
pass
The error output
Traceback (most recent call last):
File "tempfile.py", line 1, in <module>
import tempfile
File "/home/zhkzyth/codeRep/pytest/tempfile.py", line 5, in <module>
tempfile = tempfile.mktemp()
AttributeError: 'module' object has no attribute 'mktemp'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/apport_python_hook.py", line 66, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python2.7/dist-packages/apport/__init__.py", line 1, in <module>
from apport.report import Report
File "/usr/lib/python2.7/dist-packages/apport/report.py", line 12, in <module>
import subprocess, tempfile, os.path, urllib, re, pwd, grp, os
File "/home/zhkzyth/codeRep/pytest/tempfile.py", line 5, in <module>
tempfile = tempfile.mktemp()
AttributeError: 'module' object has no attribute 'mktemp'
Original exception was:
Traceback (most recent call last):
File "tempfile.py", line 1, in <module>
import tempfile
File "/home/zhkzyth/codeRep/pytest/tempfile.py", line 5, in <module>
tempfile = tempfile.mktemp()
AttributeError: 'module' object has no attribute 'mktemp'
My enviroment
ubuntu12.04
python2.7
Did you name your own file tempfile.py? If so, rename it, delete all your *.pyc files, and try again.
PS: providing the actual text of the error with the traceback would tell us these things.
Trying to access an attribute that does not belong to a class or function in a module raises an AttributeError exception, the attribute might have been deprecated in a later version of the Python interpreter being used. I suggest you check the version of the Python you're running and make sure your dir(module) includes the attribute you're trying to use