我有用于Django模型的fixture JSON文件,我想通过python脚本更新数据。我不想在终端python manage.py loaddata fixture.json
上运行命令。这我想通过python脚本来完成。通过python脚本加载fixture.json
我为Django创建了虚拟环境。我也想通过脚本启动虚拟环境。我写了小脚本,但它不起作用。
from subprocess import call
from os import chdir, getcwd
original_directory = getcwd()
chdir('/home/naresh/test_django')
call('source bin/activate', shell = True)
chdir('graphossss/')
call('python manage.py loaddata background/fixtures.json', shell= True)
chdir(original_directory)
遇到错误:
"File "manage.py", line 8, in
from django.core.management import execute_from_command_line
ImportError: No module named django.core.management"
这意味着虚拟ENV没有被激活...
你仍然得到语法错误的subprocess.call解决它。请仔细查看我的答案。 –
@MorganThrapp我试着与你的代码,但与你的代码“调用('源','斌/激活')”也给错误,但与abobe代码,它不给错误。 – Naresh
你不能只是添加你的virutalenv你的Python路径的路径? –