Why are environment variables empty in Flask apps? - 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.

Related

What is the best way to allow user to configure a Python package

I have situation like this. I am creating a Python package. That Python package needs to use Redis, so I want to allow the user of the package to define the Redis url.
Here's how I attempted to do it:
bin/main.py
from my_package.main import run
from my_package.config import config
basicConfig(filename='logs.log', level=DEBUG)
# the user defines the redis url
config['redis_url'] = 'redis://localhost:6379/0'
run()
my_package/config.py
config = {
"redis_url": None
}
my_package/main.py
from .config import config
def run():
print(config["redis_url"]) # prints None instead of what I want
Unfortunately, it doesn't work. In main.py the value of config["redis_url"] is None instead of the url defined in bin/main.py file. Why is that? How can I make it work?
I could pass the config to the run() function, but then if I run some other function I will need to pass the config to that function as well. I'd like to pass it one time ideally.

exec() python command stops the whole execution

I am trying to run a script that sequentially changes some parameters in a config file (MET_config_EEv40.cfg) and runs a script ('IS_MET_EEv40_RAW.py') that retrieves these new config parameters:
config_filename = os.getcwd() + '/MET_config_EEv40.cfg'
import sys
parser = configparser.ConfigParser()
parser.read('MET_config_EEv40.cfg')
parser.set('RAW', 'product', 'ERA')
parser.set('RAW', 'gee_product', 'ECMWF/ERA5_LAND/HOURLY')
parser.set('RAW', 'indicator', 'PRCP')
parser.set('RAW', 'resolution', '11110')
with open('MET_config_EEv40.cfg', 'w') as configfile:
parser.write(configfile)
## execute file
import sys
os.system(exec(open('IS_MET_EEv40_RAW.py').read()))
#exec(open('IS_MET_EEv40_RAW.py').read())
print('I am here')
After this execution, I get the output of my script as expected:
Period of Reference: 2005 - 2019
Area of Interest: /InfoSequia/GIS/ink/shp_basin_wgs84.shp
Raw data is up to date. No new dates available in raw data
Press any key to continue . . .
But it never prints the end line: I am here, so that means that after the execution of the script, the algorithm is terminated. That is not what I want it to do, as I would like to be able to change some other config parameters and run the script again.
That output is showed because of this line of the code:
if (delta.days<=1):
sys.exit('Raw data is up to date. No new dates available in raw data')
So could be that sys.exit is ending both processes? Any ideas to replace sys.exit() inside the code to avoid this?
Im executing this file from a .bat file that contains the following:
#echo OFF
docker exec container python MET/PRCPmain.py
pause
exec(source, globals=None, locals=None, /) does
Execute the given source in the context of globals and locals.
So
import sys
exec("sys.exit(0)")
print("after")
is same as writing
import sys
sys.exit(0)
print("after")
which obviously terminate and does not print after.
exec has optional argument globals which you can use to provide your alternative to sys for example
class MySys:
def exit(self, *args):
pass
exec("sys.exit(0)",{"sys":MySys()})
print("after")
which does output
after
as it does use exit from MySys instance. If your codes make use of other things from sys and want it to work normally you would need method mimicking sys function in MySys class

Error while calling the send_file function from another file in python flask | RuntimeError: Working outside of request context

Greetings stack overflow community, I am currently working on a flask app and I am trying to retrieve a file from a helper function with the send_file method in flask.
I have a route that goes like so:
#app.route("/process",methods=['GET','POST'])
def do_something():
process = threading.Thread(target=function_name,args=[arg1,arg2])
process.start()
return render_template("template.html")
The function_name (which is on a different file) function is suposed to return a file like so
def function_name():
filename = 'ohhey.pdf'
return send_file(filename,as_attachment=True,cache_timeout=0)
When I run my app like this I get the following error
RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that needed
to interface with the current application object in some way. To solve
this, set up an application context with app.app_context(). See the
documentation for more information.
So I try to change the function for the following:
def function_name():
filename = 'ohhey.pdf'
with app.app_context():
return send_file(filename,as_attachment=True,cache_timeout=0)
and get this new error
RuntimeError: Working outside of request context.
This typically means that you attempted to use functionality that needed
an active HTTP request. Consult the documentation on testing for
information about how to avoid this problem.
so I try the following:
def function_name():
filename = 'ohhey.pdf'
with app.test_request_context():
return send_file(filename,as_attachment=True,cache_timeout=0)
After making this final change my app doesn't return a file or an error. I appreciate your help.

Reading properties from config file with Flask and Python

Building from Reading config file as dictionary in Flask I'm attempting to define a custom configuration file in order to customize my Flask app.
Running code :
from flask import Flask
app = Flask(__name__)
import os
my_config = app.config.from_pyfile(os.path.join('.', 'config/app.conf'), silent=False)
#app.route('/')
def hello_world():
with app.open_instance_resource('app.cfg') as f:
print(type(f.read()))
return 'Hello World! {}'.format(app.config.get('LOGGING'))
if __name__ == '__main__':
app.run()
In config/app.conf these properties are added :
myvar1="tester"
myvar2="tester"
myvar3="tester"
Invoking the service for hello_world() function returns :
Hello World! None
Shouldn't the contents of the file be returned instead of None ?
I attempt to access a property using app.config.get('myvar1') but None is also returned in this case.
From the official Flask documentation:
The configuration files themselves are actual Python files. Only values in uppercase are actually stored in the config object later on. So make sure to use uppercase letters for your config keys.
Therefore, set structure your ./config/app.conf variables as:
VARIABLE_NAME = "variable value"
Load as:
app.config.from_pyfile(os.path.join(".", "config/app.conf"), silent=False)
NOTE: If you're not using instance specific configuration you don't actually need to build the path as it will take the Flask app path as its root.
And access the values later as:
app.config.get("VARIABLE_NAME")

File is not created/opened when using with open

I am using Flask and testing some code in Python. I am trying to store in a log file a Flask request and a string every time a post is done.
This is my code:
from flask import Flask, render_template, request
from vsearch import search4letters
app = Flask(__name__)
def log_request(request, results: str) -> None:
print(request)
with open('vsearch.log', 'a') as log:
print(request, results, file=log)
#app.route('/search4', methods=['POST'])
def do_search() -> 'html':
phrase = request.form['phrase']
letters = request.form['letters']
title = 'Here are your results:'
results = str(search4letters(phrase, letters))
log_request(request, results)
return render_template('results.html',
the_phrase=phrase,
the_letters=letters,
the_title=title,
the_results=results,
)
#app.route('/')
#app.route('/entry')
def entry_page() -> 'html':
return render_template('entry.html',
the_title='Welcome to search4letters on the web!')
if __name__ == '__main__':
app.run(debug=True)
This is my HTML view:
After pressing Do it!, 'vsearch.log' should contained what I have printed to it, but it does not. In addition, when the file does not exists, it does not get created.
I have tried changing the mode of open to 'a+', but I get the same results. I have also made a debug, and these lines are just executed with no errors raised.
Could somebody explain me what is going on, and how can I solve this problem ?
Since you're using Flask it's much better to use the built in logging functionality. See: http://flask.pocoo.org/docs/0.12/errorhandling/#logging-to-a-file
So, for example, on app startup you'd have:
import logging
file_handler = logging.FileHandler('/path/to/your/flask.log')
file_handler.setLevel(logging.WARNING)
app.logger.addHandler(file_handler)
Then wherever you want to log something in your application you'd log to warning or above, or whatever you set the file handler log level to:
#app.route('/whatever')
def whatever():
app.logger.warning('Whatever!')
return render_template('whatever.html')
Thanks to #AlexHall, I have been able to solve this problem. The solution is to specify the full absolute path to the file.
def log_request(request, results: str) -> None:
with open('/absolute/path/to/the/file/vsearch.log', 'a') as log:
print(request, results, file=log)
In addition, following #AlexHall suggestion to know the current working directory. I have seen that this is:
/Applications/PyCharm.app/Contents/bin
so when not specifying the full absolute path the file 'vsearch.log' was created here.
EDIT:
So, it seems that the problem was I was running my code from PyCharm. However, when I use the terminal and I just run:
$ python webapp.py
I do not need to specify the full absolute path.
EDIT:
I was able to solve this issue, and I probably screwed up the settings at some point, but after deleting all the run configurations in PyCharm, and running the program from webapp.py everything has been solved.
I really want to thank #AlexHall since he gave me all tips to solve this problem.

Categories

Resources