简体   繁体   English

Python错误:AttributeError:'module'对象没有属性

[英]Python error: AttributeError: 'module' object has no attribute

I'm totally new to Python and I know this question was asked many times, but unfortunately it seems that my situation is a bit different... I have created a package (or so I think).我对 Python 完全陌生,我知道这个问题被问了很多次,但不幸的是,我的情况似乎有点不同......我已经创建了一个包(或者我认为是这样)。 The catalog tree is like this:目录树是这样的:

mydir
    lib   (__init__.py)
        mod1  (__init__.py, mod11.py)

In parenthesis there are files in the catalog.括号中是目录中的文件。 Both __init__.py files are zero length. __init__.py文件的长度都为零。

The file mydir/lib/mod1/mod11.py contains the following:文件mydir/lib/mod1/mod11.py包含以下内容:

def mod12():
    print "mod12"

Now, I run python , then import lib , which works OK, then lib.mod11() or lib.mod12() .现在,我运行python ,然后import lib ,它工作正常,然后是lib.mod11()lib.mod12()

Either of the last two gives me the subject error message.最后两个中的任何一个都给了我主题错误消息。 Actually dir(lib) after Step 2 does not display mod11 or mod12 either.实际上,步骤 2 之后的dir(lib)也不显示mod11mod12 It seems I'm missing something really simple.看来我错过了一些非常简单的东西。

(I'm using Python 2.6 in Ubuntu 10.10) (我在 Ubuntu 10.10 中使用 Python 2.6)

Thank you谢谢

When you import lib , you're importing the package.当您import lib ,您正在导入包。 The only file to get evaluated and run in this case is the 0 byte __init__.py in the lib directory.在这种情况下,唯一要评估和运行的文件是 lib 目录中的 0 字节__init__.py

If you want access to your function, you can do something like this from lib.mod1 import mod1 and then run the mod12 function like so mod1.mod12() .如果你想访问你的函数,你可以from lib.mod1 import mod1做这样的事情,然后像mod1.mod12()一样运行mod12函数。

If you want to be able to access mod1 when you import lib , you need to put an import mod1 inside the __init__.py file inside the lib directory.如果您希望在导入lib时能够访问mod1 ,则需要在lib目录内的__init__.py文件中放置一个import mod1

More accurately, your mod1 and lib directories are not modules, they are packages.更准确地说,您的mod1lib目录不是模块,而是包。 The file mod11.py is a module.文件mod11.py是一个模块。

Python does not automatically import subpackages or modules. Python 不会自动导入子包或模块。 You have to explicitly do it, or "cheat" by adding import statements in the initializers.您必须明确地这样做,或者通过在初始值设定项中添加导入语句来“欺骗”。

>>> import lib
>>> dir(lib)
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__']
>>> import lib.pkg1
>>> import lib.pkg1.mod11
>>> lib.pkg1.mod11.mod12()
mod12

An alternative is to use the from syntax to "pull" a module from a package into you scripts namespace.另一种方法是使用from语法将模块从包中“拉”到脚本命名空间中。

>>> from lib.pkg1 import mod11

Then reference the function as simply mod11.mod12() .然后将该函数简单地引用为mod11.mod12()

The way I would do it is to leave the __ init__.py files empty, and do:我这样做的方法是将 __ init__.py 文件留空,然后执行以下操作:

import lib.mod1.mod11
lib.mod1.mod11.mod12()

or

from lib.mod1.mod11 import mod12
mod12()

You may find that the mod1 dir is unnecessary, just have mod12.py in lib.您可能会发现 mod1 目录是不必要的,只需在 lib 中有 mod12.py。

My solution is put those imports in __init__.py of lib:我的解决方案是将这些导入放在 lib 的__init__.py中:

in file: __init__.py
import mod1

Then,然后,

import lib
lib.mod1

would work fine.会工作得很好。

暂无
暂无

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

相关问题 Jython:子流程模块错误,AttributeError:'module'对象没有属性'python' - Jython: Error in subprocess module, AttributeError: 'module' object has no attribute 'python' Python Pandas错误:AttributeError:“模块”对象没有属性“格式” - Python Pandas Error: AttributeError: 'module' object has no attribute 'formats' Python错误:AttributeError:'模块'对象没有属性'heappush' - Python error : AttributeError: 'module' object has no attribute 'heappush' Python 2.7错误:“ AttributeError:'模块'对象没有属性'Unit'” - Python 2.7 error: “AttributeError: 'module' object has no attribute 'Unit'” AttributeError:'module'对象没有属性'subscribe'Python - AttributeError: 'module' object has no attribute 'subscribe' Python AttributeError:'module'对象没有属性'call':Python - AttributeError:'module' object has no attribute 'call' :Python Python-> AttributeError:“模块”对象没有属性“主” - Python -> AttributeError: 'module' object has no attribute 'main' python AttributeError:“模块”对象没有属性“ monthcalendar” - python AttributeError: 'module' object has no attribute 'monthcalendar' python 属性错误:“模块”object 没有属性“fft” - python attributeerror: 'module' object has no attribute 'fft' Python:AttributeError:'module'对象没有属性'socketpair' - Python: AttributeError: 'module' object has no attribute 'socketpair'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM