I have dev environment and production for running various Python
scripts used in crontab
.
In dev environment, I usually test scripts from commandline in script directory such as "python myscript.py"
.
Now, some scripts load config from JSON
files in the same or subdirectory of script directory.
In dev, I can therefore refer to file just like that:
printconf = Config('printing.json')
However, once the script is ready for production, it is put in crontab
, and crontab
calls scripts from root
, therefore breaking the above line.
Additionally, dev and production are obviously in different places in the filesystem, so I can't even use absolute paths, because they won't be the same.
As described in How can I find script's directory with Python?, I can use various methods to find the file current directory. They mean, however, extra processing and I was wondering,
if any Python
version might have (or might have planned) any additional built-in way to tell that the file must be found relatively to running script's directory? Something like __location__
?
In essence, something that would work for file references like importing modules already does.
In addition, I have tried to add a global __location__
variable via sitecustomize.py
, but that doesn't even work.
sitecustomize.py:
if '__file__' in globals():
import os
_location_ = os.path.join(os.getcwd(), os.path.dirname(__file__))
But that doesn't work either, because:
- __location__
is not passed to the script,
- and __file__
refers to sitecustomize.py