Skip to content Skip to sidebar Skip to footer

Changing Absolute Path To Relative Paths Due To Web Deployment (python - Flask)

I've created an app that works locally and I would like to deploy it to Heroku. As I deploy it I get error in the Heroku logs about not finding the folders that I specified in my s

Solution 1:

As per Heroku Documentation you can read that:

A stack is an operating system image that is curated and maintained by Heroku. Stacks are typically based on an existing open-source Linux distribution, such as Ubuntu.

The code example you provided shows us that you have used file path names specific for Windows, and this may cause problems when you try to run your code on other platforms.

To avoid this kind of problems and make paths platform-independent you should use os.path which takes care of it.

You could use for example in your server.py:

# absolute path to this fileFILE_DIR = os.path.dirname(os.path.abspath(__file__))
# absolute path to this file's root directoryPARENT_DIR = os.path.join(FILE_DIR, os.pardir) 

and then:

dir_of_interest = os.path.join(PARENT_DIR, 'src')

Read more about os.path.join to see how to handle the example in MonKeyGenerator.py

If you are using Python 3.4+ you could also have a look at pathlib. This should give you some instinct on how to follow with the rest of your code example. I hope this helps.

Post a Comment for "Changing Absolute Path To Relative Paths Due To Web Deployment (python - Flask)"