Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django sys.path.append for project *and* app needed under WSGI

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

Project base 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 my that 'myapp' can not be imported, even though 'djapp.settings' is. (validating 'djapp' was imported)

It al runs properly with the ./manage.py command. there's a __init__ in the project folder.

For testings sake, I see the same issue using addsitedir:

site.addsitedir('/home/user/web/project/')
site.addsitedir('/home/user/web/project/djapp')
like image 593
GerardJP Avatar asked Mar 25 '10 13:03

GerardJP


People also ask

What is WSGI Django?

Django's primary deployment platform is WSGI, the Python standard for web servers and applications. Django's startproject management command sets up a minimal default WSGI configuration for you, which you can tweak as needed for your project, and direct any WSGI-compliant application server to use.

Do you need Apache with Django?

Django will work with any version of Apache which supports mod_wsgi. The official mod_wsgi documentation is your source for all the details about how to use mod_wsgi. You'll probably want to start with the installation and configuration documentation.


2 Answers

Since djapp (the django project folder) is in a parent folder that also belongs to the deployment I renamed the djapp folder simply 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'

The complete folder layout being:

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

And what have you. This way project can always be called project :)

Hope that helps.

GrtzG

like image 156
GerardJP Avatar answered Oct 10 '22 00:10

GerardJP


Presumably you've got code within your project which is doing from myapp import foo.

Two options:

  • change that to from djapp.myapp import foo, which is not recommended as it prevents portability;
  • only add djapp in your WSGI, and set the DJANGO_SETTINGS_MODULE to just 'settings'.
like image 22
Daniel Roseman Avatar answered Oct 10 '22 00:10

Daniel Roseman