我有一个python脚本来运行我的数据库。由于需要依赖关系,我通常在shell_plus内运行脚本。有没有办法将脚本加载到shell_plus中,然后在没有实际打开shell_plus接口的情况下运行我的linux命令行中的所有内容?从命令行通过shell_plus运行python脚本
4
A
回答
2
2
你打赌!
我甚至不推荐使用shell_plus。我倾向于将我的实用程序脚本存储在我的应用实用程序文件夹中然后,我只需从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))
HTH!
+0
我刚刚使用这个脚本,它很棒。我一直在编程Django一年,虽然我变得相当不错,但我从来没有在命令行级别上玩过。我的网站已准备好投入生产,并且需要一些Python cron作业。我使用这个脚本,并且运行得非常好。感谢rh0dium – Rich
相关问题
- 1. 从Windows命令行运行Python脚本,参数未通过
- 2. 通过命令运行脚本
- 3. 通过Windows命令行运行Python脚本
- 4. 从vim命令行运行vim脚本
- 5. 如何从命令行运行脚本?
- 6. 从命令行执行python脚本,Linux
- 7. 脚本运行良好时,从命令行运行,但不通过cron
- 8. 在python脚本中运行cmake命令
- 9. 运行bash命令到python脚本
- 10. Python脚本来运行Django的命令
- 11. 使用参数通过ssh使用python脚本的命令运行脚本
- 12. 如何在命令行中通过脚本运行函数?
- 13. gdb从命令行运行,但不是从脚本运行
- 14. bash脚本从命令行运行但不是从cron运行
- 15. 如何通过python运行adb命令?
- 16. 通过Python运行Windows CMD命令
- 17. 脚本在命令运行时运行
- 18. 从Windows的命令行运行python脚本
- 19. 使用命令行参数从C#运行Python脚本
- 20. 使用Pypy Interpreter从命令行运行Python脚本
- 21. 如何从命令行运行多个Python脚本?
- 22. 导入python模块以启用脚本从命令行运行
- 23. 如何从IDLE命令行运行Python脚本?
- 24. 如何通过脚本/命令行
- 25. 通过命令行选项CSH脚本
- 26. 脚本运行从超过了Cybermation命令行
- 27. 从php运行脚本exec命令
- 28. 从bash脚本运行几个命令
- 29. 从PHP脚本运行Drush命令
- 30. 从shell脚本运行ssh命令
我试着运行$ DJANGO_SETTINGS_MODULE = myproject.settings PYTHONPATH = $ HOME/djangoprojects python myscript.py,但它似乎仍然找不到我的模型类。有什么我可能会失踪? – prostock