简体   繁体   English

AttributeError:'module'对象没有属性'[x]'

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

I am trying to make a script that moves all the .txt files in your desktop to desktop/org, the code is as follows: 我正在尝试创建一个脚本,将桌面上的所有.txt文件移动到desktop / org,代码如下:

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' . 如果我执行.py,我会收到此错误: AttributeError:'module'对象没有属性'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. 但是,如果我擦除最后一行“main()”并将此脚本作为模块导入python命令行,我从那里调用.main()它可以很好地工作。 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 我正在使用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 . 因此,正在执行的第一行是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 . 所以shutil导入tarfile ,导入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 . copy有尝试导入这个好处PyStringMap从模块调用org.python.core Now, this module usually does not exist, which would cause copy to use some alternative code instead: PyStringMap = None . 现在,这个模块通常不存在,这会导致copy使用一些替代代码: PyStringMap = None

The problem is, that there is something called org : Your own script, org.py . 问题是, 一些所谓的org :你自己的剧本, org.py So what happens is that Python tries to find something called python.core.PyStringMap in your org.py . 所以会发生什么是Python试图在你的org.py找到一个叫做python.core.PyStringMaporg.py To be able to go that far, it needs to execute the script, including the main() call at the end: 为了能够走得那么远,它需要执行脚本,包括最后的main()调用:

  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. 这导致我们到shutil.copy行,这是对shutil模块的调用。 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 . 由于这是我们仍在导入的模块(从第一行开始!),它的导入尚未完全,因此内部的copy函数不存在,从而导致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. 您可以通过将脚本重命名为其他内容来轻松解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM