我有 config.py ,可以从 cli python 脚本和 Django view.py 导入
如何知道 config.py 中的什么进程导入了它?
Django 中可能有一些特殊的变量吗?
I tried
if sys.stdin.isatty():
***
但它似乎没有做我期望的事情。
我不能使用
if __name__ == '__main__':
因为config.py是导入到cli的主文件中的。
对于 Cli,我需要使用 argparse 解析命令行参数。
如果是 Django,我需要导入预定义的 django-config.py
我认为最简单的方法是检查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])
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)