# 方法一
from distutils.sysconfig import get_python_lib
print(get_python_lib())
# 方法二
import sys
=
print(sys.path)
方法一

方法二

import os
# 1.返回绝对路径
os.path.abspath(path)
# 2.返回文件名
os.path.basename(path)
# 3.返回文件路径
os.path.dirname(path)
# 4.路径存在则返回True,路径损坏返回False
os.path.exists(path)
# 5.判断路径是否为文件
os.path.isfile(path)
# 6.判断路径是否为目录
os.path.isdir(path)
# 当前文件名
print(__file__)
# 当前文件名的绝对路径
print(os.path.abspath(__file__) )
# 返回当前文件的路径
print(os.path.dirname(os.path.abspath(__file__) ))
region_file = 'xxx.conf'
CONFIG_PATH = os.path.dirname(os.path.abspath(__file__))
file = os.path.join(CONFIG_PATH, region_file)
CONFIG_File = './../conf/spider.conf'
print(os.path.abspath(CONFIG_File))
所以自定义的文件名千万不要与第三方库、内置模块同名!
import os
import sys
sys.path.append(os.path.abspath('../../'))
print(sys.path)
函数调用存在两种方式:
1.绝对导入,name为完整路径str,package为None。
2.相对导入,package需指定对应包位置。