从命令行通过shell_plus运行python脚本
我有一个python脚本,我运行它来填充我的数据库。由于需要依赖关系,我通常在shell_plus中运行脚本。有没有一种方法可以将脚本加载到shell\u plus并从我的linux命令行运行所有内容,而不必实际打开shell\u plus接口?当然 我甚至不推荐使用shell_plus。我倾向于将我的实用程序脚本存储在我的应用程序实用程序文件夹中。然后,我只需从cron作业或根据需要手动调用它们。这是我基于的框架脚本。(稍微简化)从命令行通过shell_plus运行python脚本,python,django,Python,Django,我有一个python脚本,我运行它来填充我的数据库。由于需要依赖关系,我通常在shell_plus中运行脚本。有没有一种方法可以将脚本加载到shell\u plus并从我的linux命令行运行所有内容,而不必实际打开shell\u plus接口?当然 我甚至不推荐使用shell_plus。我倾向于将我的实用程序脚本存储在我的应用程序实用程序文件夹中。然后,我只需从cron作业或根据需要手动调用它们。这是我基于的框架脚本。(稍微简化) 嗯 我尝试运行$DJANGO\u SETTINGS\u MOD
嗯 我尝试运行$DJANGO\u SETTINGS\u MODULE=myproject.SETTINGS PYTHONPATH=$HOME/djangoprojects python myscript.py,但似乎仍然找不到我的模型类。有什么我可能遗漏的吗?我刚刚用了这个脚本,它太棒了。我已经为Django编程一年了,虽然我变得相当不错,但我从来没有在命令行级别进行过最初的游戏。我的站点即将投入生产,需要一些Python cron作业。我使用了这个脚本,让它运行得非常好。谢谢你
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import logging
import time
import time
import optparse
# DO NOT IMPORT DJANGO MODELS HERE - THIS NEED TO HAPPEN BELOW!!
# This needs to be able to be run when django isn't in the picture (cron) so we need
# to be able to add in the django paths when needed.
def getArgs():
"""
Simply get the options for running update people.
"""
p = optparse.OptionParser()
help = "The Python path to a settings module, e.g. "
help += "\"myproject.settings.main\". If this isn't provided, the "
help += "DJANGO_SETTINGS_MODULE environment variable will be used."
p.add_option("-s", "--settings", action = "store", type = "string",
dest = "settings", help = help)
help = "A directory to add to the Python path, e.g."
help += " \"/home/djangoprojects/myproject\"."
p.add_option("-y", "--pythonpath", action = "store", type = "string",
dest = "pythonpath", help = help)
p.add_option("-v", "--verbose", action = "count", dest = "verbose",
help = "Turn on verbose debugging")
p.set_defaults(settings = os.environ.get("DJANGO_SETTINGS_MODULE",
"settings"),
pythonpath = "", verbose = 0,
)
return p.parse_args()
def update(opt, loglevel=None):
"""
This is the main script used for updating people
"""
start = time.time()
# This ensures that our sys.path is ready.
for path in opt.pythonpath.split(":"):
if os.path.abspath(path) not in sys.path and os.path.isdir(path):
sys.path.append(os.path.abspath(path))
os.environ['DJANGO_SETTINGS_MODULE'] = opt.settings
from django.conf import settings
try:
if settings.SITE_ROOT not in sys.path: pass
except ImportError:
return("Your setting file cannot be imported - not in sys.path??")
# IMPORT YOUR CODE MODELS HERE
from apps.core.utility.ExampleExtractor import ExampleExtractor
# YOUR DJANGO STUFF GOES HERE..
example = ExampleExtractor(loglevel=loglevel, singleton=not(opt.multiple))
raw = example.get_raw()
results = example.update_django(raw)
log.info("Time to update %s entries : %s" % (len(results), time.time() - start))
return results
if __name__ == '__main__':
logging.basicConfig(format = "%(asctime)s %(levelname)-8s %(module)s \
%(funcName)s %(message)s", datefmt = "%H:%M:%S", stream = sys.stderr)
log = logging.getLogger("")
log.setLevel(logging.DEBUG)
opts, args = getArgs()
sys.exit(update(opts))