Python 脚本不是 运行 来自 django shell
Python Script not running from django shell
我是 运行 来自 Django shell 的 Python 脚本。在我添加 main()
函数之前,它工作正常。现在它不起作用。当我执行脚本时,没有任何反应(没有错误或任何事情)。我做错了什么?
从 Django shell,我执行这样的脚本:execfile('create_xml.py')
create_xml.py:
import os
import unicodedata
from django.conf import settings
from django.template.loader import render_to_string
from databank.models import Registration
def create_xml(registrations):
for registration in registrations:
xml = render_to_string('databank/metadata.xml', {'registration': registration})
filename = "metadata%s.xml" % (registration.dataset.dataset_id)
file_path = settings.XML_DATA_FILES
with open(os.path.join(file_path, filename), 'w') as f:
f.write(xml.encode('UTF-8'))
def main():
while True:
print "For which datasets would you like to create XML files?"
dsid = input("1: All datasets\t2: Public datasets only\t3: A specific dataset- ")
if ds == 1:
# all datasets
print "Creating XML files for all datasets."
registrations = Registration.objects.all()
create_xml(registrations)
print "All done!"
elif ds == 2:
# only public datasets
print "Creating XML files for public datasets only."
registrations = Registration.objects.filter(dataset__status='public')
create_xml(registrations)
print "All done!"
elif ds == 3:
dsid = input("Please input a dataset id: ")
try:
r = Registration.objects.get(dataset__dataset_id=dsid)
print "Creating XML file for dataset %d." % (dsid)
registrations = [r]
create_xml(registrations)
print "All done!"
except:
print "Not a valid dataset id. please try again."
else:
print "Not a valid option."
if __name__ == "__main__":
main()
发生这种情况是因为 __name__ == '__main__'
只有当 python 解释器是 运行 文件作为程序时才 True
。尝试在程序中打印 __name__
,然后在更改比较后打印 execfile()
。您将找出 __name__
变量。检查 What does if __name__ == "__main__": do?
我是 运行 来自 Django shell 的 Python 脚本。在我添加 main()
函数之前,它工作正常。现在它不起作用。当我执行脚本时,没有任何反应(没有错误或任何事情)。我做错了什么?
从 Django shell,我执行这样的脚本:execfile('create_xml.py')
create_xml.py:
import os
import unicodedata
from django.conf import settings
from django.template.loader import render_to_string
from databank.models import Registration
def create_xml(registrations):
for registration in registrations:
xml = render_to_string('databank/metadata.xml', {'registration': registration})
filename = "metadata%s.xml" % (registration.dataset.dataset_id)
file_path = settings.XML_DATA_FILES
with open(os.path.join(file_path, filename), 'w') as f:
f.write(xml.encode('UTF-8'))
def main():
while True:
print "For which datasets would you like to create XML files?"
dsid = input("1: All datasets\t2: Public datasets only\t3: A specific dataset- ")
if ds == 1:
# all datasets
print "Creating XML files for all datasets."
registrations = Registration.objects.all()
create_xml(registrations)
print "All done!"
elif ds == 2:
# only public datasets
print "Creating XML files for public datasets only."
registrations = Registration.objects.filter(dataset__status='public')
create_xml(registrations)
print "All done!"
elif ds == 3:
dsid = input("Please input a dataset id: ")
try:
r = Registration.objects.get(dataset__dataset_id=dsid)
print "Creating XML file for dataset %d." % (dsid)
registrations = [r]
create_xml(registrations)
print "All done!"
except:
print "Not a valid dataset id. please try again."
else:
print "Not a valid option."
if __name__ == "__main__":
main()
发生这种情况是因为 __name__ == '__main__'
只有当 python 解释器是 运行 文件作为程序时才 True
。尝试在程序中打印 __name__
,然后在更改比较后打印 execfile()
。您将找出 __name__
变量。检查 What does if __name__ == "__main__": do?