Reputation: 695
I have python script python_proj.py
. There're few prints and simple functions. It calls a flask script:
os.system("flask run")
Flask script is in the same directory. Everything works when I run these from terminal in current directory:
user123@user123:~/PycharmProjects/python_and_bash$ ./python_proj.py
But when I'm in parent directory (for example '~'
) and I try to run this script:
user123@user123:~$ ./PycharmProjects/python_and_bash/python_proj.py
It doesn't work and gives me this error:
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
Usage: flask run [OPTIONS]
Error: Could not locate a Flask application. You did not provide the "FLASK_APP" environment variable, and a "wsgi.py" or "app.py" module was not found in the current directory.
How to do it? I need to send this app for non-technical mates. It should work everywhere and everytime
Upvotes: 2
Views: 2527
Reputation: 695
I added these lines in my proj_python.py script. Now it works.
path = pathlib.Path(os.path.realpath(__file__))
path = str(path.parent) + "/app.py"
os.system(path)
Be aware! Don't put os.system("flask run")
after these lines! It will cause that flask script will start 2 times! First start will be caused by: os.system(path)
, after ctrl+c
or interrupt signal os.system("flask run")
will start second time.
Upvotes: 0
Reputation: 56
You could find the absolute path of the directory of your script using this code:
import pathlib
parent_folder = pathlib.Path(__file__).parent.absolute() # Get the absolute path of parent folder
os.environ['FLASK_APP'] = parent_folder / 'your_flask_app.py' # Set environment variable
os.system("flask run")
Upvotes: 2