加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python导入,路径,目录和模块

发布时间:2020-12-16 22:56:36 所属栏目:Python 来源:网络整理
导读:首先让我说我已经对过去一周的过程进行了广泛的研究,但还没有找到这些问题的实际答案 – 只是一些模糊的答案并没有真正解释正在发生的事情.如果这只是因为我错过了我想要的东西,我很抱歉 – 请指出我正确的方向. 我的目录结构是: TestProject/ runtest* tes
首先让我说我已经对过去一周的过程进行了广泛的研究,但还没有找到这些问题的实际答案 – 只是一些模糊的答案并没有真正解释正在发生的事情.如果这只是因为我错过了我想要的东西,我很抱歉 – 请指出我正确的方向.

我的目录结构是:

TestProject/
    runtest*
    testpackage/
        __init__.py
        testmod.py
        testmod2.py
        testsubs/
            testsubmod.py

几个笔记:

>我在Ubuntu上使用python2.7
>我正在测试bpython
>我正在从特定目录运行bpython来测试导入行为的方式
>我正在尝试遵循最佳做法.
>此软件包未安装,它位于随机开发目录中
>此目录不在pythonpath中
>我在包目录中有一个init.py.
>嵌套目录中没有init.py文件
> init.py文件为空
> testpackage / testmod.py包含TestModClass
> testpackage / testsubs / testsubmod.py包含TestSubModClass

我观察到的事情:

>当我从TestProject / import testpackage运行bpython时

>这不会导入testpackage.testmod
>我根本无法访问testpackage.testmod

>当我从TestProject / import testpackage.testmod运行bpython失败时
>当我从testProject运行bpython /从testpackage导入testmod工作
>我可以向init.py添加代码以显式导入testmod.py,但不能testubs / testmod.py

>我不认为这是正确的方法,如果用户不想导入该模块怎么办?

>从testmod.py我可以导入testmod2,但不能导入testpackage.testmod2

>这样做很好,我可以使用STL或扭曲的名称(例如testpackage.logging)命名我自己的模块而不会导致错误(很糟糕的是必须将自己的模块命名为客户日志,而不仅仅是mypackage .logging)

问题是:

> python与包装上的进口有什么不同吗? pythonpath中存在的模块比尝试从当前目录导入时要多?
>为什么不导入testpackage让我访问testpackage.testmod?当我导入操作系统时,我可以访问os.path(etc).
>使用包,我应该坚持在基目录中使用单个init.py,还是应该将它们嵌套在后续目录中?
>如何导入指定包名称的模块? I.E.从testmod.py,我想导入testpackage.testmod2而不仅仅是testmod2.
>从subsubs目录导入子模块的正确方法是什么?

>我看到的唯一解决方案是将该目录从init.py附加到pythonpath,但我不知道这是否正确.

提前致谢.

解决方法

首先,您将在 section 6 of The Python Tutorial找到所需的所有信息.

(1) Does python deal differently with imports on packages & modules that exist in the pythonpath than when you are trying to import from your current directory?

不,它没有.实际上,Python在导入模块时总是搜索sys.path.仅找到当前目录中的模块,因为sys.path包含带有空字符串的条目,表示当前目录.

(2) Why doesn’t import testpackage give me access to testpackage.testmod? When I import os,I can then access os.path (etc).

为了提高效率,import testpackage只加载testpackage / __ init__.py.如果你需要testpackage.testmod,你必须明确地导入它:

import testpackage   # Just imports testpackage,not testpackage.testmod!
import testpackage.testmod   # Import *both* testpackage and testpackage.testmod!

如果你总是想导出testmod,在__init__.py中导入它,这就是os(os / __ init__.py)所做的.这样,如果导入testpackage,testpackage.testmod总是隐含可用.

由于Python是跨平台的,因此实际上无法在目录中一致地自动加载模块,因为某些文件系统不区分大小写(Windows!). Python不知道是否将os / path.py加载为os.path或os.Path等.

(3) With a package,should I stick to using a single __init__.py in the base directory,or should I be nesting them in subsequent directories?

每个子包总是需要__init__.py.有关于放弃这一要求的讨论,但决定保持原样.

(4) How can I import a module specifying the package name? I.E. from testmod.py,I would like to import testpackage.testmod2 rather than just testmod2.

这应该工作.只需确保从顶级目录运行代码.如果当前目录是testpackage,则testmod不知道它在包中.

但首选的方法是使用相对的包内导入:

from . import testmod2

如果存在名为testmod2的全局模块,则可以防止名称冲突,并且可以使用包中的已知模块的名称而不会出现问题.

(5) What is the proper way to import submodules from the subsubs directory?
The only solution I see is to append that directory to the pythonpath from __init__.py,but I don’t know if that’s the correct way.

不,不要那样做!当其中一个父目录已经在sys.path中时,永远不要将目录放到sys.path中!这可能导致您的模块加载两次,这是一件坏事!

通常,您应该能够使用绝对或相对导入从子包加载模块:

import testpackage.testsubs.testsubmod
from testpackage.testsubs import testsubmod
from .testsubs import testsubmod

只需确保在testsubs /中创建__init__.py!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读