Search code examples
djangovirtualenvdjango-southdjango-manage.py

Django not finding apps in virtualenv when using manage.py syncdb


My problem is in getting manage.py syncdb to run within a virtualenv.

It was working fine at one point, but seems to have broken sometime around when I installed South and updated pip and distribute.

Anyways, when the virtualenv is activated, I can import apps fine within the interactive interpreter. Running through mod_wsgi, the apps are imported as well, and the site can run.

When I run manage.py syncdb, it fails to find any app in INSTALLED_APPS that is in my virtualenv. It picks up system-installed apps fine, but fails when it tries to import virtualenv only apps.


Solution

  • Hi This is an old question, but saw its not answered. Not sure what you are attempting to do, but there are basically two modes you can use virtualenv,

    1. For development, to create self-contained environments
    2. For deployment, to create self-contained environments

    In the first case, you need to first Activate your virtualenv with source venv/bin/activate, for when you deploy, you need to ensure that the virtualenv is activated for your website code. Personally i prefer the following approach to ensuring your path is set correctly. (I also add this to my manage.py when doing development, so i dont have to worry about activating the environment first.

    Modified manage.py

    #!/usr/bin/env python
    import os.path
    
    # Cater for Virtual env, add to sys.path
    pwd = os.path.abspath(os.path.dirname(__file__))
    project = os.path.basename(pwd)
    new_path = pwd.strip(project)
    activate_this = os.path.join(new_path,'venv','bin','activate_this.py')
    execfile(activate_this, dict(__file__=activate_this))
    
    from django.core.management import execute_manager
    try:
        import settings # Assumed to be in the same directory.
    except ImportError:
        import sys
        sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
        sys.exit(1)
    
    if __name__ == "__main__":
        execute_manager(settings)
    

    This works, due to how i structure my projects, you would have to change it to your directory structure. My projects are structured like so:

    TopLevelDir
    |
    |- Project DIR
    |- venv
    |- requirements 
    |- deployment configs