osx yosemite - django dev on mac having to explicitly name full path -
after long time away app wrote in django , didn't complete, i've come on new mac.
i'm struggling code refer apps , files within them without explicit path. instance:
from myproject.app.file import object
whereas remember not having use myproject
every time.
is has changed? seem remember being add path in manage.py
called every time run dev server, hasn't worked time.
sys.path.append /path/to/myproject
should fix issue i'm having?
i started simple answer , grew more details on how add subdirectories of project python path. maybe bit off-topic, useful i'm pushing post button anyway.
i have bunch of small re-usable apps of mine keep inside project tree, because don't want them grow independent modules. projet tree this:
manage.py myproject/apps myproject/libs myproject/settings ...
still, default, django adds project root python path. yet makes no sense in opinion have apps load modules full path:
from myproject.apps.author.models import author myproject.libs.rest_filters import filters
that's both way verbose, , breaks reusability use absolute imports. not mention if someday build actual python package out of of libs, break.
so, took following steps. added relevant folders path:
# in manage.py root = os.path.dirname(__file__) sys.path.append(os.path.realpath(os.path.join(root, 'myproject', 'apps'))) sys.path.append(os.path.realpath(os.path.join(root, 'myproject', 'libs')))
but must ensure packages cannot loaded root of project, or have odd issues python load copy of module. instance, isinstance(libs.foo.bar(), myproject.libs.foo.bar) == false
it's not hard though : remove __init__.py
folders add path. make sure cannot descended project.
also, django's discover runner not descend paths unless specify them manually. may fine (if every module has own test suite). or can extend runner, knows this: sample code.
Comments
Post a Comment