tags:

views:

39

answers:

3

I currently have multiple Django sites running from one Apache server through WSGI and each site has their own virtualenv with possibly slight Python and Django version difference. For each site, I want to display the Python and Django version it is using as well as from which path it's pulling the Python binaries from.

For each Django site, I can do:

import sys
sys.version

but I'm not sure if it's showing the Python that the site is using or the system's Python. Any help on that?

A: 

but I'm not sure if it's showing the Python that the site is using or the system's Python. Any help on that?

Nope.

However, if you want to know something about your Django app, do this.

  1. Use logging. Write to sys.stderr that's usually routed to errors_log by mod_wsgi. Or look inside the request for the wsgi.errors file.

  2. Update your top-level urls.py to write a startup message, including sys.version to your log.

S.Lott
A: 

To find out which Python is being used, log the value of sys.executable. It should contain the path to the interpreter being used.

Ned Deily
A: 

This is a bit more than what you asked for, but we have a similar situation where different directories are (possibly) using different Python/Django code. We use following to build an easy to look at list (at least for me) of Python & Django versions plus all modules and where they were loaded from. It has sometimes helped save what little there is left of my hair. Modify to taste.

import sys, re, os
import django

def ModuleList():
    ret = []
    # The double call to dirname() is because this file is in our utils/ 
    # directory, which is one level down from the top of the project.
    dir_project = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
    project_name = os.path.basename(dir_project)

    for k,v in sys.modules.items():

        x = str(v)
        if 'built-in' in x:
            ret.append((k, 'built-in'))
            continue

        m = re.search(r"^.*?'(?P<module>.*?)' from '(?P<file>.*?)'.*$", x)
        if m:
            d = m.groupdict()
            f = d['file']
            # You can skip all of the re.sub()'s if you want raw paths
            f = re.sub(r'/usr/.*?/lib/python[.0-9]*/site-packages/django/', 'system django >> ', f)
            f = re.sub(r'/usr/.*?/lib/python[.0-9]*/site-packages/', 'site-packages >> ', f)
            f = re.sub(r'/usr/.*?/lib/python[.0-9]*/', 'system python >> ', f)
            f = re.sub(dir_project+'.*python/', 'local python >> ', f)
            f = re.sub(dir_project+'.*django/', 'local django >> ', f)
            f = re.sub(dir_project+r'(/\.\./)?', project_name + ' >> ', f)
            ret.append((d['module'], f))
    ret.sort( lambda a,b: cmp(a[0].lower(), b[0].lower()) )
    ret.insert(0, ('Python version', sys.version) )
    ret.insert(0, ('Django version', django.get_version()) )

    return ret
# ModuleList
Peter Rowell