While providing feedback to participants of Online Seminar of Informatics I’ve encountered a code, which used sys.path[0]
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[0]
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[0]:", sys.path[0])
print("sys.argv[0]:", sys.argv[0])
app = Flask(__name__)
app.run(debug=True)
sys.path[0]: c:\tmp\flask_sys
sys.argv[0]: 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[0]:
sys.argv[0]: C:\tmp\flask_sys\app.py
More on sys.path
for example in this great tutorial.