• python之import子目录文件


    问题:

     

    在pre_tab.py文件下:

    print("AA")
    from test.te import login1
    login1()
    from test.te import login1
    程序中此句引入当前目录下test目录中的te.py文件中的login1对象(方法)
    但是一直报错 importError 没找到test.te这个模块
     
    Traceback (most recent call last):
      File "C:/Users/Administrator/PycharmProjects/laonanhai/shop_store/pre_tab.py", line 21, in <module>
        from test.te import login1
    ImportError: No module named 'test.te'
    查找原因是test目录下没有创建__init__.py文件(其实默认是有创建的,但我原先删了)
     
     

    The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.

     渣渣谷哥翻译:
    需要__init__.py文件来使Python将目录视为包含包; 这样做是为了防止具有公用名称的目录(例如字符串)意外隐藏模块搜索路径上稍后出现的有效模块。 在最简单的情况下,__init__.py可以只是一个空文件,但它也可以执行包的初始化代码或设置__all__变量,稍后描述。
     
     

    解决方法:

    在test目录下建__init__().py文件
     
     
    补充别人的看法;
      如官网所述,此文件使得这个目录被包含到当前的python package中,
      我理解就是成为一个module,import查找当前目录时这个目录能被识别了,然后import成功.
     
     
     
    作者:前程明亮
    出处:http://www.cnblogs.com/0zcl
    文章未标明转载则为原创博客。欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.

    1.非系统的学习也是在浪费时间
    2.做一个会欣赏美,懂艺术,会艺术的技术人
  • 相关阅读:
    dedecms调用指定栏目名称,链接
    修改 Dedecms 文档文章标题长度的方法
    dedeCMS的arclist标签中limit是什么意思
    DedeCms织梦系统[field:description /]标签如何限制字数?
    mysql 如何更改root密码
    允许phpmyadmin空密码登录的配置方法
    Apache启动失败解决办法
    Apache 配置多域名、二级域名
    使用Auto Layout中的VFL(Visual format language)--代码实现自动布局
    使用UITextField自动格式化银行卡号
  • 原文地址:https://www.cnblogs.com/0zcl/p/5971719.html
Copyright © 2020-2023  润新知