python入门-PyCharm中目录directory与包package的区别及相关import详解

时间:2022-09-25 21:40:53

一、概念介绍

在介绍目录directory与包package的区别之前,先理解一个概念---模块

模块的定义:本质就是以.py结尾的python文件,模块的目的是为了其他程序进行引用。

目录(Dictionary):

Dictionary在pycharm中就是一个文件夹,放置资源文件,对应于在进行JavaWeb开发时用于放置css/js文件的目录,或者说在进行物体识别时,用来存储背景图像的文件夹。该文件夹其中并不包含_ _ init.py_ _文件

包(Package):

对于Python package 文件夹而言,与Dictionary不同之处在于其会自动创建__init__.py文件。 
简单的说,python package就是一个目录,其中包括一组模块和一个__init__.py文件。

二、导入模块

导入模块的方法:

import module_name
import module1_name,module2_name
from module_name import * ---> 一般import * 不建议使用
from module_name import m1,m2,m3 ---> m1为module_name下面的方法或变量
from module_name import logger as logger_a ---> 为导入的变量或方法取个别名,引用时直接用别名

1.同级目录下模块的导入:

python入门-PyCharm中目录directory与包package的区别及相关import详解

在main_day41.py中导入para_day41.py,两种方法:

#方法一: 相当于把para_day41.py中的所有代码拷贝过来赋值给变量para_day41,引用时直接用"该变量."
import para_day41
para_day41.show_para() 运行结果:
D:\python365\python3.exe D:/Pyexample/20190220Day4/main_day41.py
in the para_day41
#方法二: 只把para_day41.py中name变量和show_para方法拷贝过来,可以直接用(推荐用该方法)
from para_day41 import name,show_para
print(name)
show_para() 运行结果:
D:\python365\python3.exe D:/Pyexample/20190220Day4/main_day41.py
para_day41
in the para_day41

2.不同级目录下模块的导入:

(1)导入子目录下的模块

python入门-PyCharm中目录directory与包package的区别及相关import详解

main.py中导入day51目录下面para_day51.py

import day51.para_day51
day51.para_day51.show_para()
from day51.para_day51 import *
show_para()
from day51 import para_day51
para_day51.show_para() 运行结果:
D:\python365\python3.exe D:/Pyexample/20190220Day5/main.py
in the para_day51

(2)导入上级目录下的模块

python入门-PyCharm中目录directory与包package的区别及相关import详解

day22目录下面的main_day22.py导入day21目录下的para_day21.py

注:执行main_day22.py时,要导入day21目录下的para_day21.py,具体的搜索路径是这样的:

  首先,python需要在当前目录下去找para_day21.py,发现没有,然后就到sys.path列表中的每个路径下面去找:

['D:\\Pyexample\\20190220Day2\\day22', 'D:\\Pyexample', 'D:\\Pyexample\\W3', 'D:\\Pyexample\\20181113', 'D:\\Pyexample\\CSP\\cspmonitor', 'D:\\Pyexample\\python', 'D:\\python365\\python36.zip', 'D:\\python365\\DLLs', 'D:\\python365\\lib', 'D:\\python365', 'D:\\python365\\lib\\site-packages']

  结果仍没找到,所以就会报错:

Traceback (most recent call last):
File "D:/Pyexample/20190220Day2/day22/main_day22.py", line 21, in <module>
import day21.para_day21
ModuleNotFoundError: No module named 'day21'

  解决办法:需要手动将para_day21.py所在的父目录或上上级目录加到sys.path列表中,让python可以搜索到即可

import sys,os
print(sys.path) p = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) #获取要导入模块的上上级目录
print(p) sys.path.insert(0,p) #把获取到的上上级目录加到sys.path列表中 from day21 import para_day21
print(para_day21.name)
para_day21.show_para() 运行结果:
['D:\\Pyexample\\20190220Day2\\day22', 'D:\\Pyexample', 'D:\\Pyexample\\W3', 'D:\\Pyexample\\20181113', 'D:\\Pyexample\\CSP\\cspmonitor', 'D:\\Pyexample\\python', 'D:\\python365\\python36.zip', 'D:\\python365\\DLLs', 'D:\\python365\\lib', 'D:\\python365', 'D:\\python365\\lib\\site-packages']
D:\Pyexample\20190220Day2
['D:\\Pyexample\\20190220Day2', 'D:\\Pyexample\\20190220Day2\\day22', 'D:\\Pyexample', 'D:\\Pyexample\\W3', 'D:\\Pyexample\\20181113', 'D:\\Pyexample\\CSP\\cspmonitor', 'D:\\Pyexample\\python', 'D:\\python365\\python36.zip', 'D:\\python365\\DLLs', 'D:\\python365\\lib', 'D:\\python365', 'D:\\python365\\lib\\site-packages']
para_day21
in the para_day21

或者把要导入模块的父目录加到sys.path列表中

p = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + '\\day21'  #获取要导入模块的父目录
sys.path.insert(0,p)
import para_day21
print(para_day21.name)
para_day21.show_para()

三、导入包

导入包的本质就是执行这个包下面的__init__.py文件

1.同级路径下包的导入:

python入门-PyCharm中目录directory与包package的区别及相关import详解

main.py中导入包day31,直接用import 包名即可

import day31

(1)__init__.py中没有内容时

D:\python365\python3.exe D:/Pyexample/20190220Day3/main.py

Process finished with exit code 0

(2)__init__.py中有内容时:

python入门-PyCharm中目录directory与包package的区别及相关import详解

D:\python365\python3.exe D:/Pyexample/20190220Day3/main.py
in the day31 __init__ Process finished with exit code 0

2.不同级路径下包的导入:

python入门-PyCharm中目录directory与包package的区别及相关import详解

前提:需要将要导入的包所在的路径添加到sys.path列表中,方法同上,不再赘述。

3. 导入包中的模块

只是导入一个包并没有什么实际意义,包是用来从逻辑上组织模块的,所以实际项目中更多的是导入包中的模块,导入包中模块的方法:

from Package1 import Module1
from Package1 import Package2
from Package1.Module1 import m1,m2 ---> m1,m2为模块中的变量或方法
from Package1.Module1 import *
import Package1.Module1
import Package1.Package2

要导入的包及其模块结构如下:

python入门-PyCharm中目录directory与包package的区别及相关import详解

(1)在main.py中导入包day31下面的para_day31.py或者para_day32.py模块

import day31.para_day31,day31.para_day32
day31.para_day31.show_para()
day31.para_day32.show_para() 运行结果:
D:\python365\python3.exe D:/Pyexample/20190220Day3/main.py
in the day31 __init__
in the para_day31
in the para_day32 Process finished with exit code 0

或者:

from day31 import para_day31,para_day32
para_day31.show_para()
para_day32.show_para() 运行结果:
D:\python365\python3.exe D:/Pyexample/20190220Day3/main.py
in the day31 __init__
in the para_day31
in the para_day32 Process finished with exit code 0

或者:

from day31.para_day31 import show_para,name
print(name)
show_para() 运行结果:
D:\python365\python3.exe D:/Pyexample/20190220Day3/main.py
in the day31 __init__
para_day31
in the para_day31 Process finished with exit code 0

或者:

from day31.para_day31 import *
print(name)
show_para()

(2)在main.py中导入包day31下面所有的模块,即全部导入

from day31 import *
para_day31.show_para()
para_day32.show_para() 运行结果:
D:\python365\python3.exe D:/Pyexample/20190220Day3/main.py
in the day31 __init__
Traceback (most recent call last):
File "D:/Pyexample/20190220Day3/main.py", line 35, in <module>
para_day31.show_para()
NameError: name 'para_day31' is not defined Process finished with exit code 1

此时,需要用到__init__.py文件中的__all__变量,在day31包中的__init__.py文件中定义如下:

"""
@Author:janson
@Date:2019/2/20
@File:__init__
""" print("in the day31 __init__") __all__ = ['para_day31','para_day32']

这时 from day31 import * 就会把注册在包__init__.py 文件中 __all__ 列表中的子模块和子包导入到当前作用域中来:

再次运行就不会报错了:

from day31 import *
para_day31.show_para()
para_day32.show_para() 运行结果:
D:\python365\python3.exe D:/Pyexample/20190220Day3/main.py
in the day31 __init__
in the para_day31
in the para_day32 Process finished with exit code 0