Django sys.path.append for project applications * and * needed in WSGI

Can someone give me a pointer on why I need to add the project root path to the python path, as well as the application itself in my WSGI file?

The base of the project is called "djapp", the application is called "myapp".

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..') sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../djapp') os.environ['DJANGO_SETTINGS_MODULE'] = 'djapp.settings' 

If I omit the line with "/../djapp/", the log tells me that "myapp" cannot be imported, although "djapp.settings" is. (check "djapp" has been imported)

It works correctly with the team. /manage.py. there __init__ in the project folder.

For tests, I see the same problem using addsitedir :

 site.addsitedir('/home/user/web/project/') site.addsitedir('/home/user/web/project/djapp') 
+6
django django-wsgi
source share
2 answers

Since djapp (the django project folder) is located in the parent folder, which also belongs to the deployment, I renamed the djapp folder just to project . Then this code is always correct:

 sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/..' ) sys.path.append(os.path.dirname(os.path.abspath(__file__)) + '/../project') os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings' 

Full folder directory:

 host.example.com\ etc\ bin\ project\ logs\ 

And do you have. This project can always be called a project :)

Hope this helps.

GrtzG

+4
source share

Presumably you have code in your project that runs from myapp import foo .

Two options:

  • change this to from djapp.myapp import foo , which is not recommended as it prevents portability;
  • add djapp to your WSGI and set DJANGO_SETTINGS_MODULE only 'settings' .
+3
source share

All Articles