python模块导入顺序_Python导入模块的顺序是什么?

python导入语句遇到了一些奇怪的东西。在

假设我的文件结构如下:foo\

__init__.py

bar.py

os.py

代码输入棒.py(其他文件为空)

^{pr2}$

奇怪的是当我运行python -m foo.bar时,它会打印出来foo/os.pyc

但是,当我将direcotry改为foo,并运行python -m bar时,它会打印出来/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/os.pyc

我运行脚本的两种方式有什么区别?

总之,Python导入模块的顺序是什么?

从官方文件中,我发现了一些关于这个问题的文字(它们使我更加困惑)the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path.the first item of this list, path[0], is the directory containing the script that was used to invoke the Python interpreter. If the script directory is not available (e.g. if the interpreter is invoked interactively or if the script is read from standard input), path[0] is the empty string, which directs Python to search modules in the current directory first.In fact, such references are so common that the import statement first looks in the containing package before looking in the standard module search path.

...

If the imported module is not found in the current package (the package of which the current module is a submodule), the import statement looks for a top-level module with the given name.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值