python中import某个模块时,首先会从python的内置模块中查找是否含义该模块的定义,若未查询到会从sys.path对应的模块路径查询是否含有对应模块的定义,如果搜索完成依然没有对应A模块时则抛出import的异常。
__file__ 属性储存着当前执行文件的绝对路径,sys.path 中储存着搜索路径,并且sys.path[0]中存着调用python解释器的脚本所在的目录,即执行文件所在的目录位置。
例如F:/pycharm_project/Pointnet_Pointnet2_pytorch-master/me.py文件中运行如下代码。
import os
import sys
print(__file__ + '\n')
# F:/pycharm_project/Pointnet_Pointnet2_pytorch-master/me.py
for i in sys.path:
print(i)
# F:\pycharm_project\Pointnet_Pointnet2_pytorch-master
# F:\pycharm_project\Pointnet_Pointnet2_pytorch-master
# D:\Pycharm\PyCharm 2019.3.3\plugins\python\helpers\pycharm_display
# D:\Anaconda3\envs\pytorch0.4\python36.zip
# D:\Anaconda3\envs\pytorch0.4\DLLs
# D:\Anaconda3\envs\pytorch0.4\lib
# D:\Anaconda3\envs\pytorch0.4
# D:\Anaconda3\envs\pytorch0.4\lib\site-packages
# D:\Anaconda3\envs\pytorch0.4\lib\site-packages\win32
# D:\Anaconda3\envs\pytorch0.4\lib\site-packages\win32\lib
# D:\Anaconda3\envs\pytorch0.4\lib\site-packages\Pythonwin
# D:\Pycharm\PyCharm 2019.3.3\plugins\python\helpers\pycharm_matplotlib_backend
print('\n' + sys.path[0])
# F:\pycharm_project\Pointnet_Pointnet2_pytorch-master
注:当利用终端执行该文件时,print(__file__)的结果和pycharm中不一样,结果仅显示当前文件。
import os
import sys
# print(__file__ + '\n')
#
# for i in sys.path:
# print(i)
#
# print('\n' + sys.path[0])
print(os.path.dirname(__file__))
# F:/pycharm_project/Pointnet_Pointnet2_pytorch-master
print(os.path.abspath(__file__))
# F:\pycharm_project\Pointnet_Pointnet2_pytorch-master\me.py
BASE_DIR = os.path.dirname((os.path.abspath(__file__)))
print(BASE_DIR)
# F:\pycharm_project\Pointnet_Pointnet2_pytorch-master
os.path.abspath(__file__) 获取当前当前文件的绝对路劲
os.path.dirname()获取当前文件上一层目录
注:终端执行结果与pycharm中不同。这是因为pycharm里的绝对路劲,是pyharm自作主张增加的,终端里面是不会有的。
import os
import sys
# print(__file__ + '\n')
#
# for i in sys.path:
# print(i)
#
# print('\n' + sys.path[0])
# print(os.path.dirname(__file__))
# print(os.path.abspath(__file__))
# BASE_DIR = os.path.dirname((os.path.abspath(__file__)))
# print(BASE_DIR)
# 将当前文件夹下的model文件夹添加到sys.path中
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
ROOT_DIR = BASE_DIR
sys.path.append(os.path.join(ROOT_DIR, 'models')) # os.path.join 把目录和文件名合成一个路径
for i in sys.path:
print(i)
终端输出结果:
在实际开发中,默认包含了当前目录为搜索路径,所以,当前目录下的模块和子模块均可以正常访问。但是若一个模块需要import平级的不同目录的模块,或者上级目录里面的模块,就可以通过修改path来实现。
添加执行文件所在目录的上层目录进入sys.path。
import os
import sys
# print(__file__ + '\n')
#
# for i in sys.path:
# print(i)
#
# print('\n' + sys.path[0])
# print(os.path.dirname(__file__))
# print(os.path.abspath(__file__))
# BASE_DIR = os.path.dirname((os.path.abspath(__file__)))
# print(BASE_DIR)
# for i in sys.path:
# print(i)
# print()
#
# 添加执行文件所在目录的上层目录进入sys.path
BASE_DIR = os.path.dirname(sys.path[0])
ROOT_DIR = BASE_DIR
sys.path.append(os.path.join(ROOT_DIR))
for i in sys.path:
print(i)
# F:\pycharm_project\Pointnet_Pointnet2_pytorch-master
# F:\pycharm_project\Pointnet_Pointnet2_pytorch-master
# D:\Pycharm\PyCharm 2019.3.3\plugins\python\helpers\pycharm_display
# D:\Anaconda3\envs\pytorch0.4\python36.zip
# D:\Anaconda3\envs\pytorch0.4\DLLs
# D:\Anaconda3\envs\pytorch0.4\lib
# D:\Anaconda3\envs\pytorch0.4
# D:\Anaconda3\envs\pytorch0.4\lib\site-packages
# D:\Anaconda3\envs\pytorch0.4\lib\site-packages\win32
# D:\Anaconda3\envs\pytorch0.4\lib\site-packages\win32\lib
# D:\Anaconda3\envs\pytorch0.4\lib\site-packages\Pythonwin
# D:\Pycharm\PyCharm 2019.3.3\plugins\python\helpers\pycharm_matplotlib_backend
# F:\pycharm_project