• <tfoot id='uySb7'></tfoot>
      <bdo id='uySb7'></bdo><ul id='uySb7'></ul>
      <legend id='uySb7'><style id='uySb7'><dir id='uySb7'><q id='uySb7'></q></dir></style></legend>
      <i id='uySb7'><tr id='uySb7'><dt id='uySb7'><q id='uySb7'><span id='uySb7'><b id='uySb7'><form id='uySb7'><ins id='uySb7'></ins><ul id='uySb7'></ul><sub id='uySb7'></sub></form><legend id='uySb7'></legend><bdo id='uySb7'><pre id='uySb7'><center id='uySb7'></center></pre></bdo></b><th id='uySb7'></th></span></q></dt></tr></i><div id='uySb7'><tfoot id='uySb7'></tfoot><dl id='uySb7'><fieldset id='uySb7'></fieldset></dl></div>

      <small id='uySb7'></small><noframes id='uySb7'>

      1. Python 错误:AttributeError:“模块"对象没有属性

        时间:2023-09-14

          <tfoot id='C1V5p'></tfoot><legend id='C1V5p'><style id='C1V5p'><dir id='C1V5p'><q id='C1V5p'></q></dir></style></legend>
            <tbody id='C1V5p'></tbody>

          <small id='C1V5p'></small><noframes id='C1V5p'>

            1. <i id='C1V5p'><tr id='C1V5p'><dt id='C1V5p'><q id='C1V5p'><span id='C1V5p'><b id='C1V5p'><form id='C1V5p'><ins id='C1V5p'></ins><ul id='C1V5p'></ul><sub id='C1V5p'></sub></form><legend id='C1V5p'></legend><bdo id='C1V5p'><pre id='C1V5p'><center id='C1V5p'></center></pre></bdo></b><th id='C1V5p'></th></span></q></dt></tr></i><div id='C1V5p'><tfoot id='C1V5p'></tfoot><dl id='C1V5p'><fieldset id='C1V5p'></fieldset></dl></div>

                  <bdo id='C1V5p'></bdo><ul id='C1V5p'></ul>
                  本文介绍了Python 错误:AttributeError:“模块"对象没有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我对 Python 完全陌生,我知道这个问题被问过很多次,但不幸的是,我的情况似乎有点不同......我已经创建了一个包(或者我认为).目录树是这样的:

                  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). The catalog tree is like this:

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

                  括号中是目录中的文件.两个 __init__.py 文件都是零长度.

                  In parenthesis there are files in the catalog. Both __init__.py files are zero length.

                  文件 mydir/lib/mod1/mod11.py 包含以下内容:

                  The file mydir/lib/mod1/mod11.py contains the following:

                  def mod12():
                      print "mod12"
                  

                  现在,我运行 python,然后运行 ​​import lib,运行正常,然后运行 ​​lib.mod11()lib.mod12().

                  Now, I run python, then import lib, which works OK, then lib.mod11() or lib.mod12().

                  最后两个中的任何一个都给了我主题错误消息.实际上,步骤 2 之后的 dir(lib) 也不显示 mod11mod12 .看来我错过了一些非常简单的东西.

                  Either of the last two gives me the subject error message. Actually dir(lib) after Step 2 does not display mod11 or mod12 either. It seems I'm missing something really simple.

                  (我在 Ubuntu 10.10 中使用 Python 2.6)

                  (I'm using Python 2.6 in Ubuntu 10.10)

                  谢谢

                  推荐答案

                  当你 import lib 时,你正在导入包.在这种情况下,唯一要评估和运行的文件是 lib 目录中的 0 字节 __init__.py.

                  When you import lib, you're importing the package. The only file to get evaluated and run in this case is the 0 byte __init__.py in the lib directory.

                  如果你想访问你的函数,你可以执行类似 from lib.mod1 import mod1 的操作,然后像 mod1 那样运行 mod12 函数.mod12().

                  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().

                  如果你想在导入lib的时候能够访问mod1,你需要在里面放一个import mod1lib 目录中的 __init__.py 文件.

                  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.

                  这篇关于Python 错误:AttributeError:“模块"对象没有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何编写好的/正确的包 __init__.py 文件 下一篇:如何导入在 __init__.py 中定义的类

                  相关文章

                  最新文章

                    <small id='R2b5v'></small><noframes id='R2b5v'>

                    <tfoot id='R2b5v'></tfoot>
                    <legend id='R2b5v'><style id='R2b5v'><dir id='R2b5v'><q id='R2b5v'></q></dir></style></legend>
                  1. <i id='R2b5v'><tr id='R2b5v'><dt id='R2b5v'><q id='R2b5v'><span id='R2b5v'><b id='R2b5v'><form id='R2b5v'><ins id='R2b5v'></ins><ul id='R2b5v'></ul><sub id='R2b5v'></sub></form><legend id='R2b5v'></legend><bdo id='R2b5v'><pre id='R2b5v'><center id='R2b5v'></center></pre></bdo></b><th id='R2b5v'></th></span></q></dt></tr></i><div id='R2b5v'><tfoot id='R2b5v'></tfoot><dl id='R2b5v'><fieldset id='R2b5v'></fieldset></dl></div>

                      • <bdo id='R2b5v'></bdo><ul id='R2b5v'></ul>