How to migrate a Django project to Pythonanywhere

I am trying to configure a Django application on Pythonanywhere - I was able to detect Bitbucket and clone the code - I deleted the files in the directory that was provided for me, but cannot make it work.

I made "syncdb", then when I get to what I think is the correct URL for the application, I keep getting "Unhandled Exception". The error is that it cannot find "portfolio.settings" in the import (portfolio is the name of the application)

I also do not know what to put for MEDIA_ROOT and STATIC_DIRS - they should be, as far as I know, in full ways, and not relative.

I'm new to Django, and it proves pretty overwhelming to get an application that works fine locally, deployed. Any help I provide (I haven't found Pythonanywhere forums that don't seem to be indexed, or don't help everyone, which is useful, I'm afraid)

I also thought: why I do not allow Pythonanywhere to set up an empty project for me, but again, I do not know how to handle STATIC_DIRS and MEDIA_ROOT, and I really do not know how to make my project fit their settings.

Thanks for any help.

+7
python django pythonanywhere
source share
1 answer

For everyone who encounters similar problems: import errors in web applications are usually associated with incorrect sys.path settings. Check your WSGI file (in PythonAnywhere you can find it on your web tab. Other hosts may do something different).

Example:

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

Your WSGI file should have:

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

And your settings.py parameter should have

 INSTALLED_APPS = ( #... 'my_cool_app' 

Everything should coincide with the fact that the names of the names of the points of your application in INSTALLED_APPS and your environment variable DJANGO_SETTINGS_MODULE will be imported correctly, relative to the folder that you add to sys.path.

So, in the above example, you could do:

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

But do not do it, it is more difficult.

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

+4
source share

All Articles