我有一個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. 通過python腳本運行ffmpeg命令
- 2. 從Windows命令行運行Python腳本,參數未通過
- 3. 通過命令運行腳本
- 4. 通過Windows命令行運行Python腳本
- 5. 從MEL命令運行的Python腳本
- 6. 從python腳本運行vim命令
- 7. 如何通過傳遞命令行參數通過Python運行R腳本
- 8. 從命令行運行Perl腳本?
- 9. 從命令行運行Groovy腳本
- 10. 從命令行運行Sqlite3腳本
- 11. 如何從命令行運行腳本?
- 12. 從vim命令行運行vim腳本
- 13. 從命令行執行python腳本,Linux
- 14. 腳本運行良好時,從命令行運行,但不通過cron
- 15. 通過命令行打開TOPCAT通過Python腳本
- 16. 運行bash命令到python腳本
- 17. Python腳本來運行Django的命令
- 18. 在python腳本中運行cmake命令
- 19. 從命令行運行Python
- 20. 使用參數通過ssh使用python腳本的命令運行腳本
- 21. 如何在命令行中通過腳本運行函數?
- 22. 命令從命令行運行,但不能從shell腳本運行
- 23. 從python(Django)shell中的python腳本運行python命令。 Django
- 24. gdb從命令行運行,但不是從腳本運行
- 25. Powershell腳本從命令行運行,但不是從TFS運行
- 26. bash腳本從命令行運行但不是從cron運行
- 27. 通過CGI運行sudo命令(Python)
- 28. 如何通過python運行adb命令?
- 29. 通過Python運行Windows CMD命令
- 30. 腳本在命令運行時運行
我試着運行$ DJANGO_SETTINGS_MODULE = myproject.settings PYTHONPATH = $ HOME/djangoprojects python myscript.py,但它似乎仍然找不到我的模型類。有什麼我可能會失蹤? – prostock