python 小知识

PYTHONPATH是Python搜索路径,默认我们import的模块都会从PYTHONPATH里面寻找。 使用下面的代码可以打印PYTHONPATH:

print(os.sys.path)
improt os
print os.__file__
#Result: C:\Python26\lib\os.pyc
 
方法二:
import os
import inspect
 
print inspect.getfile(os)
 
#Result: C:\Python26\lib\os.pyc
print inspect.getsourcefile(os)
#Result: C:\Python26\lib\os.py
 
方法三:
import os
import imp
print imp.find_module('os')
#Result: ((open file 'C:Python26libos.py', mode 'U' at 0x000000000000002105ed0), 'C:\Python26\lib\os.py', (.py), 'U', 1)
 
  • If the import module in the same dir, use e.g: from . import core
  • If the import module in the top dir, use e.g: from .. import core
  • If the import module in the other subdir, use e.g: from ..other import core

 https://docs.python.org/2/tutorial/modules.html

原文地址:https://www.cnblogs.com/santian/p/5238162.html