Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'module' object has no attribute '[x]'

Tags:

python

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

like image 332
Phob1a Avatar asked Mar 02 '14 17:03

Phob1a


1 Answers

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.

like image 192
poke Avatar answered Nov 14 '22 23:11

poke