set_paths

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# content of set_paths.py:
import sys, os
import path # http://www.jorendorff.com/articles/python/path/
def set_paths(f):
    p = path.path(f)
    sys.path.append(str(p.parent.joinpath("..").abspath()))
    sys.path.append(str(p.parent.parent.joinpath("lib/").abspath()))
    os.environ["DJANGO_SETTINGS_MODULE"] = "myproject.settings" 

# usage: 
# contents of file "some_script":
#!/usr/bin/python

import set_paths
set_paths.set_paths(__file__) # this must be the first thing in the script.

from myproject.myapp.models import MyModel
print MyModel.objects.count() # whatever.

More like this

  1. manage.py with magic python path by bikeshedder 6 years ago
  2. djangopath: conveniently set sys.path and DJANGO_SETTINGS_MODULE by alia_khouri 4 years, 10 months ago
  3. cron/console bootstrap django by nstrite 5 years, 10 months ago
  4. Update All Apps to Latest Revision by dedaluz 4 years, 10 months ago
  5. Dynamic import from an installed app by Archatas 4 years, 2 months ago

Comments

(Forgotten your password?)