简体   繁体   中英

changing sys.path in settings.py

In a django test project's settings.py I recently tried putting this in:

try:
    import mymodule
except:
    import sys
    TESTPROJ_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
    PROJ_PARENT = os.path.abspath(os.path.join(TESTPROJ_ROOT, '..'))
    sys.path.append(PROJ_PARENT)

The purpose was to add a local project path if mymodule wasn't installed in the environment. To aid understanding, the directory structure is as follows:

base/mymodule
base/test_project/manage.py
base/test_project/test_project/settings.py

This worked so long as mymodule wasn't in the environment. As soon as import mymodule was executed (anywhere in settings.py ) I got the error:

django.core.exceptions.ImproperlyConfigured: ImportError django.contrib.admin: cannot import name signals

I have two questions...

a) is there any way to stop this error from happening?

b) is there another 'smart' way to allow settings.py to change the path to allow a module to be locally included if it's not in the environment?

Thanks in advance.

You can create a file my-site.pth in a directory that is a part of sys.path , and include in that file the full path to the directory you want to add.

my-site.pth

base/mymodule

http://docs.python.org/2/library/site.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM