Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to migrate Django project to Pythonanywhere

I am trying to set up a Django app on Pythonanywhere — I've managed to figure out Bitbucket and clone the code in — I deleted the files in the directory that was provided for me — but can't get it to work.

I've done 'syncdb', then when I go to what I think is the correct URL for the app, I keep getting "Unhandled exception" — The error is that it can't find 'portfolio.settings' in an import (portfolio is the name of the app)

I also have no idea what to put for MEDIA_ROOT and STATIC_DIRS — these should be, as far as I know, full paths, not relative.

I'm a Django newbie, and this is proving rather overwhelming, to get the app, which functions fine locally, deployed. Any help much provided (I haven't found the Pythonanywhere forums — which don't seem indexed -- or help all that helpful, I'm afraid)

I also thought: why don't I let Pythonanywhere set up a blank project for me, but again, I don't know how to handle STATIC_DIRS and MEDIA_ROOT, and I don't really know how to make my project fit their setup.

Thanks for any help.

like image 754
Cerulean Avatar asked Mar 27 '14 23:03

Cerulean


People also ask

How do I find django version in PythonAnywhere?

Checking the django installationpython -m django --version # This should show something like 4.1. x # If it shows a version lower than 4.1, you've probably forgotten to activate your virtualenv!


1 Answers

For anyone else that runs into similar issues: import errors in web applications are typically to do with your sys.path not being correctly configured. Check your WSGI file (on PythonAnywhere, you can find it on your web tab. Other hosts may do things differently).

Example:

  • /home/myusername/myproject/ is the project folder
  • /home/myusername/myproject/my_cool_app/ is one of your app folders
  • /home/myusername/myproject/myproject/settings.py is the settings file location

Your WSGI file should have:

sys.path.append('/home/myusername/myproject')
# ...
DJANGO_SETTINGS_MODULE = 'myproject.settings'

And your settings.py should have

INSTALLED_APPS = (
   #...
   'my_cool_app'

Everything has to line up so that the dot-notation names of your app in INSTALLED_APPS and your DJANGO_SETTINGS_MODULE environment variable will import correctly, relative to the folder you add to sys.path.

So in the example above, you could conceivably do:

# wsgi file
sys.path.append('/home/myusername')
DJANGO_SETTINGS_MODULE = 'myproject.myproject.settings'
# settings.py
INSTALLED_APPS = 'myproject.my_cool_app'

But don't do that, it's overcomplicated.

PS there's a detailed guide to sys.path and import issues for pythonanywhere in the docs.

like image 169
hwjp Avatar answered Sep 29 '22 21:09

hwjp