Kartik Rokde
Kartik Rokde

Reputation: 3913

Why we need to specify absolute path in Bottle + uWSGI?

I'm developing a Bottle application. My program reads configurations from configuration (.cfg) files and also using template (.tpl) files.

But when I host my app on nginx using uWSGI, it cannot find the files (given relative paths to the project)

What is the possible solution??

Upvotes: 1

Views: 668

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1122292

Instead of using relative paths, store an absolute path based on the current module path:

MODULEPATH = os.path.dirname(__file__)

template = open(os.path.join(MODULEPATH, 'templates/sometemplate.tpl').read()

__file__ is the filename of the current module, os.path.dirname(__file__) is the directory the module resides in.

You should never rely on relative paths in Python code; the current working directory is not changed when running a python program.

Upvotes: 4

Related Questions