2022年 11月 7日

Python 路径设置

#1:设置python模块的搜索路径有几种方式

'''
1、设置PYTHONPATH环境变量
2、通过添加.pth文件
3、通过sys.path设置路径
'''


#2:永久设置python模块的搜索路径有几种方式,如何使用他们
'''
1、在python安装路径下添加*.pth文件,在文件中添加需要的路径
'''

#3:如何临时设置python模块的搜索路径
import sys
sys.path.append('.\\modules')

import working_fun
print(working_fun.greet('Tim'))

'''
1、windows系统,在命令行执行时,使用 set PYTHONPATH path 
'''

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

modules\working_fun.py

def greet(name):
    return 'hello ' + name
  • 1
  • 2