Injecting Stuff into your Python Path

Similar to a previous post where I wrote about how to run flask tests without installing your app, another common thing that you might want to be able to do is import your app from some arbitrary script.

This is especially useful when running your app with apache mod_wsgi. This module expects the app to be installed globally or at least in the python path. Unless you install the app in a traditional sense this will not be true.

The solution is just to inject the path prior to running your import statement like this.

sys.path.insert(0, '/var/www/blog')
from blog import app as application

This import will actually work.

If you made it this far, you should probably follow me on twitter. 🙂

This entry was posted in Hacking and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *