run.py
from flask import Flask
app = Flask(__name__)
flask的源码:
def _get_package_path(name):
try:
return os.path.abspath(os.path.dirname(sys.modules[name].__file__))
except (KeyError, AttributeError):
return os.getcwd()
class Flask:
def __init__(self, name):
self.name = name
self.root_path = _get_package_path(self.name)
这其中 _get_package_path 这么写也能获得run.py的根目录:
def _get_package_path(name):
try:
return os.path.abspath(os.path.dirname(name))
except (KeyError, AttributeError):
return os.getcwd()
为什么要加上sys.modules[name].__file__