While providing feedback to participants of Online Seminar of Informatics I’ve encountered a code, which used
sys.path to get the current working directory.
sys.path is a list of folders used by Python interpreter during import resolving.
For simple cases
sys.path is indeed the current working directory, but beware that there are a lot of things, which can change the
sys.path variable - automatic reload of a Flask server is one of them.
The following is my minimal reproducer to show the behavior.
from flask import Flask import sys print("sys.path:", sys.path) print("sys.argv:", sys.argv) app = Flask(__name__) app.run(debug=True)
sys.path: c:\tmp\flask_sys sys.argv: c:\tmp\flask_sys\app.py * Serving Flask app "app" (lazy loading) # ... Change the python file and save it, it triggers automatic reload. * Restarting with stat sys.path: sys.argv: C:\tmp\flask_sys\app.py
sys.path for example in this great tutorial.