我在Django应用程序中执行脚本时遇到问题。 此脚本必须用于crontab中的作业。 生病提供我的脚本示例:
规范:Python:3.5.x Django:1.10.5
my_script.py
class SayHello(object): def print_args(self, arg1, arg2): print (arg1, arg2) if __name__ == "__main__": foo = SayHello() foo.print_args(sys.argv[1], sys.argv[2])但主要的问题是当我想在这个脚本中包含模型时我得到错误: ImportError:没有名为“app”的模块
文件夹结构:
say_hello(主文件夹)
- > init .py
- > my_script.py
如何运行脚本但不会从import语句中获取错误到此脚本中。 任何建议都会很棒。
I have problem with executing script in Django application. This script must serve for job in crontab. So ill provide example of my script:
Specification: Python:3.5.x Django:1.10.5
my_script.py
class SayHello(object): def print_args(self, arg1, arg2): print (arg1, arg2) if __name__ == "__main__": foo = SayHello() foo.print_args(sys.argv[1], sys.argv[2])But the main problem is when i want to include models in this script i got error: ImportError: No module named "app"
Folder structure:
say_hello (main folder)
-> init.py
-> my_script.py
How to run script but don't get errors from import statement into this script. Any advice would be great.
最满意答案
您的脚本必须位于应用程序的文件夹管理/命令/中。
脚本示例:
# -*- coding: utf-8 -*- # example.py from django.core.management.base import BaseCommand class Command(BaseCommand): def handle(self, *args, **options): print "Hello word!!"要运行脚本:
./manage.py example这里有文档https://docs.djangoproject.com/en/1.10/howto/custom-management-commands/
You script must be in the folder management/commands/ of the app.
Script example:
# -*- coding: utf-8 -*- # example.py from django.core.management.base import BaseCommand class Command(BaseCommand): def handle(self, *args, **options): print "Hello word!!"To run the script:
./manage.py exampleHere you have the documentation https://docs.djangoproject.com/en/1.10/howto/custom-management-commands/
更多推荐
发布评论