我有可以从cli python脚本和Django view.py
导入的config.py如何在config.py中了解 - 导入了哪个进程? Django可能有一些特殊的变量吗?
我试过
public static void selectSpinnerItemByValue(Spinner spnr, long value) {
SpinnerAdapter adapter = spnr.getAdapter();
for (int position = 0; position < adapter.getCount(); position++) {
if(adapter.getItemId(position) == value) {
spnr.setSelection(position);
return;
}
}
}
但似乎没有做我预期的事情。
我无法使用
if sys.stdin.isatty():
***
因为config.py被导入cli中的主文件。
对于Cli,我需要使用argparse解析命令行参数。 在Django的情况下,我需要导入预定义的django-config.py
答案 0 :(得分:3)
我认为最简单的方法是检查sys.argv
。
import sys
# sys.argv[0] Should output your script name in a command line environment
# It should output wsgi.py if you're using wsgi, etc.
if sys.argv[0] == 'manage.py':
print('called with manage.py, I\'m on the command line!')
elif sys.argv[0] == 'mod_wsgi':
print('called with with ]mod_wsgi, I\'m run by an HTTP server!')
# In the case where you have something other than Apache with mod_wsgi running your server
# you'll need to manually determine the contents of sys.argv[0]
# elif sys.argv[0] == '<your-servers-response-here>':
# print('called by your HTTP server!')
else:
print('called with %s, I don\'t know where that is!' % sys.argv[0])
答案 1 :(得分:0)
我会尝试背负django设置:
try:
from django.conf import settings
x = settings.DATABASES['default']['name'] == 'the name of your default database'
from_django = True
except ImportError, AttributeError, KeyError:
from_django = False