a.py,aa.py,aa.py,b.py,bb.py,bbb.py,c.py,cc.py和ccc.py都类似:

# a.py
info = '我是文件a'
print(info)

fileA\__init__.py:

print('导入fileA子包...')
var_A = "我是fileA中的字符串变量..."

fileC\__init__.py:

print('导入fileC子包...')
var_C = "我是fileC中的字符串变量..."
import fileC.c
from fileC import cc
from . import ccc

cmd控制台下的操作演示:

Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

尝试新的跨平台 PowerShell https://aka.ms/pscore6

PS C:\Users\chenxuqi\Desktop\新建文件夹\分析__init__.py作用> python     
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul  8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.  
>>> fileA
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'fileA' is not defined
>>> import fileA
导入fileA子包...
>>> fileA
<module 'fileA' from 'C:\\Users\\chenxuqi\\Desktop\\新建文件夹\\分析__init__.py作用\\fileA\\__init__.py'>
>>> fileA.a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'fileA' has no attribute 'a'
>>> import fileA.a
我是文件a
>>> fileA.a
<module 'fileA.a' from 'C:\\Users\\chenxuqi\\Desktop\\新建文件夹\\分析__init__.py作用\\fileA\\a.py'>
>>> fileA.a.info
'我是文件a'
>>> fileA.aa
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'fileA' has no attribute 'aa'
>>> fileA.var_A
'我是fileA中的字符串变量...'
>>> fileB
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'fileB' is not defined
>>> import fileB
>>> fileB
<module 'fileB' (namespace)>
>>> fileB.b
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'fileB' has no attribute 'b'
>>> import fileB.b
我是文件b
>>> fileB.b
<module 'fileB.b' from 'C:\\Users\\chenxuqi\\Desktop\\新建文件夹\\分析__init__.py作用\\fileB\\b.py'>
>>> fileB.bb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'fileB' has no attribute 'bb'
>>> fileB.b.info
'我是文件b'
>>> fileC
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'fileC' is not defined
>>> import fileC
导入fileC子包...
我是文件c       
我是文件cc      
我是文件ccc     
>>> file.var_C
Traceback (most recent call last):   
  File "<stdin>", line 1, in <module>
NameError: name 'file' is not defined
>>> fileC.var_C
'我是fileC中的字符串变量...'
>>> fileC.c
<module 'fileC.c' from 'C:\\Users\\chenxuqi\\Desktop\\新建文件夹\\分析__init__.py作用\\fileC\\c.py'>
>>> fileC.cc
<module 'fileC.cc' from 'C:\\Users\\chenxuqi\\Desktop\\新建文件夹\\分析__init__.py作用\\fileC\\cc.py'>
>>> fileC.ccc
<module 'fileC.ccc' from 'C:\\Users\\chenxuqi\\Desktop\\新建文件夹\\分析
__init__.py作用\\fileC\\ccc.py'>
>>> fileC.ccc.info
'我是文件ccc'
>>>
>>> 

结论:利用__init__.py文件,可以赋予子包一些属性,包括变量,或者是该子包下的模块,只要在__init__.py文件下定义变量和导入相应模块即可,非常方便.如果在__init__.py文件中没有导入,那么如果引用这个子包内的模块会报错,比如:AttributeError: module 'fileB' has no attribute 'b',这个时候我们需要自己导入,比如:import fileB.b,这样我们才可以正常使用fileB.b这个变量.

注意,使用相对路径导入时,不要到达打开文件夹的顶层,否则会报错,ValueError: attempted relative import beyond top-level package.即,如果vscode打开的文件夹是Folder1,那么使用相对路径的时候不要到达Folder1,最高只能到达Folder1目录的子目录.

文件file1\file2\test4cxq.py:

import file1.file2.b2
from . import bb2
from ..file2 import bbb2
from .. import b1
import fileB.b
import fileB.bb
import fileB.bbb

文件fileB\test4cxq.py:

import fileB.b
from . import bb

# from ..fileC import c
# 以上代码会出错,不能到达顶层目录,否则会报错
r'''
    from ..fileC import c
ValueError: attempted relative import beyond top-level package
'''
import fileA.a
import fileA.aa
import fileA.aaa

尝试运行'./test.py文件,查看运行结果:'

import file1.file2.test4cxq
import fileB.test4cxq
Windows PowerShell
版权所有 (C) Microsoft Corporation。保留所有权利。

尝试新的跨平台 PowerShell https://aka.ms/pscore6

PS C:\Users\chenxuqi\Desktop\新建文件夹\discussion>  & 'D:\Python\Python37\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2020.11.371526539\pythonFiles\lib\python\debugpy\launcher' '55816' '--' 'c:\Users\chenxuqi\Desktop\新建文件夹\discussion\test.py'
我是文件b2
我是文件bb2
我是文件bbb2
我是文件b1
我是文件b
我是文件bb
我是文件bbb
导入fileA子包...
我是文件a
我是文件aa
我是文件aaa
PS C:\Users\chenxuqi\Desktop\新建文件夹\discussion> 

源代码获取: 下载链接

本文地址:https://blog.csdn.net/m0_46653437/article/details/109884531