我试图在我的django项目中独立运行一个脚本。我正在使用Django 1.6,这是一个开发服务器,所以没有使用uwsgi,我在python虚拟服务器上运行。虚拟环境对我来说似乎很好,因为我可以运行python runserver 0.0.0.0:9000而没有任何问题。
这是我的环境设置:
(bot)one@chat-dash /home/git/bot_server/bot_server/bot_server $ export DJANGO_SETTINGS_MODULE=bot_server.settings.local
(bot)one@chat-dash /home/git/bot_server/bot_server/bot_server $ echo $DJANGO_SETTINGS_MODULE
bot_server.settings.local
这是我的剧本:
(bot)one@chat-dash /home/git/bot_server/bot_server/bot_server/bot_data $ cat req.py
import sys
import os
import datetime
import base64
import json
from generators import thread_generator
from do_request import do_request
os.environ['DJANGO_SETTINGS_MODULE'] = "bot_server.settings.local"
from .models import User, ThreadVault
以下是run:
的输出(bot)one@chat-dash /home/git/bot_server/bot_server/bot_server/bot_data $ python req.py
Traceback (most recent call last):
File "req.py", line 10, in <module>
from bot_data.models import User, ThreadVault
ImportError: No module named bot_data.models
(bot)one@chat-dash /home/git/bot_server/bot_server/bot_server/bot_data $
答案 0 :(得分:0)
我发现如果bot_data的外部/父级,脚本将正确运行。我需要弄清楚为什么bot_data / req.py的路径将它带到了Django环境之外。