为什么Flask应用程序中的环境变量为空? [英] Why are environment variables empty in Flask apps?
问题描述
my_app.py:
我有一个瓶子应用程序(my_app)调用另一个文件(my_function) / p>
从my_functions导入my_function
@ app.route('/')
def index():
my_function()
return render_template('index.html')
my_functions.py:
def my_function():
try:
import my_lib
除了
print(my_lib not found in system!)
#do stuff ...
如果__name__ ==__main__:
my_function()
当我直接执行my_functions.py(即python my_functions.py)my_lib 进口没有错误;然而,当我执行烧瓶应用程序(即python my_app.py)时,我得到my_lib的导入错误。
当我开始打印LD_LIBRARY_PATH变量时的每个文件:
print(os.environ ['LD_LIBRARY_PATH'])
调用my_functions.py时,我得到正确的值,但调用my_app.py时没有值(空)。尝试在开始时设置此值of my_app.py不起作用:
os.environ ['LD_LIBRARY_PATH'] ='/ usr / local / lib'
问题:
(1)为什么在Flask应用程序中调用时,LD_LIBRARY_PATH为空?
(2)如何设置?
任何帮助赞赏
解决方案LD_LIBRARY_PATH在执行烧瓶应用程序时被清除,可能出于安全原因迈克建议。为了解决这个问题,我使用子进程直接拨打一个可执行文件:
$
call_str =executable_name -arg1 arg1_value -arg2 arg2_value
subprocess.call(call_str,shell = True,stderr = subprocess.STDOUT)
理想情况下,程序应该能够使用python绑定,但现在调用可执行文件。
I have a flask app (my_app) that calls a function in a different file (my_function):
my_app.py:
from my_functions import my_function @app.route('/') def index(): my_function() return render_template('index.html')
my_functions.py:
def my_function(): try: import my_lib except: print("my_lib not found in system!") # do stuff... if __name__ == "__main__": my_function()
When I execute my_functions.py directly (i.e., python my_functions.py) "my_lib" is imported without error; however, when I execute the flask app (i.e., python my_app.py) I get an import error for "my_lib".
When I print the LD_LIBRARY_PATH variable at the beginning of each file:
print(os.environ['LD_LIBRARY_PATH'])
I get the correct value when calling my_functions.py, but get no value (empty) when calling my_app.py.Trying to set this value at the beginning of my_app.py has no effect:
os.environ['LD_LIBRARY_PATH'] = '/usr/local/lib'
Questions:
(1) Why is 'LD_LIBRARY_PATH' empty when called within the Flask app?
(2) How do I set it?
Any help appreciated.
解决方案LD_LIBRARY_PATH is cleared when executing the flask app, likely for security reasons as Mike suggested.
To get around this, I use subprocess to make a call directly to an executable:
import subprocess call_str = "executable_name -arg1 arg1_value -arg2 arg2_value" subprocess.call(call_str, shell=True, stderr=subprocess.STDOUT)
Ideally the program should be able to use the python bindings, but for now calling the executable works.
这篇关于为什么Flask应用程序中的环境变量为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!