Using a Python SDK, I have developed an application that I run from a terminal, since some library I used required me to run this command on sudo.
Application creates a folder by creating a project name, and stores all data in the folde
In my case, when I run sudo python app.py, I am getting root directory instead of home directory /home/<user_name>/
In Python, how do I get the /home/user/ ?
Please note that I only have one user.
I think that what you want is os.getlogin() or even better getpass.getlogin(). According to the standard library documentation for os.login():
For most purposes, it is more useful to use getpass.getuser() since the latter checks the environment variables LOGNAME or USERNAME to find out who the user is, and falls back to pwd.getpwuid(os.getuid())[0] to get the login name of the current real user id.
So if you want to pay attention to the environment variables, use getpass.getlogin() else prefere os.getlogin()
os.path.expanduser('~{user}')
Where {user} is any user, in your case...user:
os.path.expanduser('~user')
cd /home/user && sudo python3 main.py
format, it will run in whatever directory you want.
I just started learning flask and I am stuck at setting up the Flask environment variables. I don't know how to setup the environment variables. Whenever I use the flask run command, I encounter the following error:
Error message : 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.
I did a lot of google searches to setup environment variables on Windows but I am unable to find a solution and sometimes I am not able to understand the solution.
How to do this ? How to get the "app.py" or "wsgi.py" ?
Windows PowerShell
set FLASK_APP=hello.py
$env:FLASK_APP = "hello.py"
flask run
you need to provide an application environment. So Flask needs to know, the .py file to run.
Try to run that command
export FLASK_APP=application.py
where application.py - name of your Flask app in my case it is application.py.
after that
flask run
I used this and it worked because I am doing it in Windows PowerShell.
$env:FLASK_APP = "app.py"
however, flask run didn't and gave me could not import application.
My error was also same but fortunately I was able to resolve it. Here you go,
D:\Development\Projects\Python_Projects\flask_blog>set FLASK_APP=app.py
D:\Development\Projects\Python_Projects\flask_blog>$env:FLASK_APP = "app.py"
D:\Development\Projects\Python_Projects\flask_blog>python -m flask run
hope this could help someone,
first set flask env like this inside the python virtual env
(for windows command prompt)
set FLASK_ENV=development
then
set FLASK_APP=app.py
(app.py should be your flask application file)
I don't think the 'flask run' command is the one which causes the error here.
I got the same message error and the problem came from the fact I copied/pasted the set FLASK_APP and $env: FLASK_APP commands as written in the documentation. I had to add spaces before and after '>' or '=', and then everything worked.
Example: this command didn't work 'C:\path\to\app>set FLASK_APP=hello.py', but this one did 'C:\path\to\app > set FLASK_APP = hello.py'.
Maybe it's the same problem you have?
You need to actually run it from your Windows command line, NOT the built in command line for something like Visual Studio Code. Run those commands from your windows command line, in the proper directory, and everything should work.
The reason this creates problems - Visual Studio Code creates a Powershell environment for your command line. You could use the recommended $env:FLASK_APP = "your_app.py" from within the VSC environment and that will work too.
A bit late but I hope this helps others!!!
I was facing the same problem and the thing that worked for me was don't put spaces before and after the = sign.
For example: FLASK_APP = flaskblog.py is wrong and will likely give you an error because of the spaces before and after the = sign.
Instead try FLASK_APP=flaskblog.py
It worked for me.
You can try this
set FLASK_APP=application.py
flask run
If you're using powershell, make sure you add quotations when setting the environment variable:
$env:FLASK_APP = "app.py
Then flask run should work.
A step-wise solution is provided below:
Go to the folder where you have placed your flask app (on the command line)
Create a virtual environment as using the command ($ py -m venv env) here 'venv' is the short form of the virtual environment and 'env' at the end represents the name of the environment which you want (I have named it as env). Thereafter you can see at from the file explorer that a folder named 'env' is created in the folder stated at point #1 above.
Enter the following command ($env\Scripts\activate) by pressing enter this will turn on your virtual environment
Thereafter, enter the following command ($set FLASK_APP=<your app name>.py)
Enter the following command ($flask run)
The set command works but to setup the environment, you need to make sure that you are in the right directory where the file is located. For example, if my application is hello_world.py and it is located at the venv\hello\hello_world.py, you need to make sure that you are in the right directory before setting up set FLASK_APP=hello_world.py this is for windows but in another OS, you need to use export instead set
(venv) C:\Users\myProjects\venv\hello\set FLASK_APP=hello_world.py
You're typing in the commands that look correct. It may be a Windows security item that your user cannot make changes to environmental variables. If you're on Windows 10, search "View Advanced System Settings." Then click environmental variables, hit new user variable and make it FLASK_APP and set the path where it asks. Then do flask run in terminal.
To add: if your terminal is just not cooperating with your environment variables, you can just call the method inside the script itself, avoiding this error completely.
from flask import Flask
app = Flask(__name__)
#your decorators, etc here
if __name__=='__main__':
app.run()
Then when you're in your activated virtual environment, all that's needed is
$python flaskblog.py
I tried a few ways mentioned in this thread so thanks everyone for your inputs.This is what worked for me.
1st, making sure you're running it when you're on the main .py file;
2nd, if you're on a Windows, use set FLASK_APP="your_file.py" (don't forget the quotation marks)
and then flask db init to initialize
...woo so happy my app finally worked!
I resolved similar problem by running the Command Prompt with admin rights (Windows+R --> enter cmd --> Hold the keys Ctrl+Shift+Enter) and then running the below command:
set FLASK_APP=<ProgramName>.py
The similar error was appearing when I was trying to run the application. I have to change the path. I changed directory to the folder where hello.py was saved.
(venv) C:\Users\win10\flask-tutorial\myproject>cd venv
(venv) C:\Users\win10\flask-tutorial\myproject\venv>set FLASK_APP=hello.py
(venv) C:\Users\win10\flask-tutorial\myproject\venv>flask run
You need to specify the environment of the application. Like this
export FLASK_APP = application.py
after performing this operation
flask run
But my suggestion is that it will be easier for you to perform these operations there while creating your application, rather than constantly stating this in the terminal. After reviewing the documentation, if you do what I said, it will be enough to come to the terminal and run python app.py runserver in terminal.
You can check flask's documentation for that.
https://flask.palletsprojects.com/en/1.1.x/patterns/appfactories/
I just had the same problem on Windows, using the command line. And problem was that I wasn't setting my app as the one that flask needs to run.
So, in my case, I turned the debug mode on first (not required but convenient) with:
set FLASK_ENV=development
then:
set FLASK_ENV=server.py
#where server.py is the name of my "app"
and finally ran flask:
flask run
Remove space between FLASK_APP and flaskblog.py, then execute flask run command or just run the program like other program python flaskblog.py
I had the same issue. These steps helped me:
Delete existing venv environment, then in terminal type in project folder path py -3 venv venv
Then: venv\Scripts\activate
pip install flask
set FLASK_APP=hello
flask run
I was facing the same issue when doing flask run in the VScode.
But when I tried on CMD it worked fine
DO one thing
from flask import Flask
app = Flask(__name__)
#app.route('/')
def hello_world():
return 'Hello World!'
app.run(debug=True)
python .\app.py
As Windows user, what helped me:
set FLASK_APP=appname.py | in CMD
$env:FLASK_APP="appname.py" | in PowerShell or VS Code(your code editing app)
And next line: flask run
for me adding this line at the end of the code fix it
if __name__=='__main__':
app.run()
so complete code would be this:
for more solution check this out
common error with running flask in windows
In the command line, you'll run three lines of code. The first two lines tell the terminal where to find your application and to run it in development mode, which allows you to keep it running while it hotloads any modifications. The third actually starts the application.
# For Mac/Linux
export FLASK_APP=app.py
export FLASK_ENV=development
flask run
# For Windows
set FLASK_APP=app.py
set FLASK_ENV=development
flask run
set FLASK_APP=app.py
python -m flask run
i use it and working hope helpful
I had a same problem but i solve the issue by pasting the code in the python file.
if __name__=="__main__":
app.run(debug=True)
I am trying to install the Google Cloud SDK on my Windows machine. I have Python 2.7 currently installed on this machine, and it's located in the System Variables Path like this -> C:\Python27\;
I am getting this error during installation:
ERROR: gcloud failed to load: DLL load failed: %1 is not a valid Win32
application.
The error message also prompts me to check the Python executable by saying:
If it is not, please set the CLOUDSDK_PYTHON environment variable to
point to a working Python 2.7 executable.
So, I'm trying to set the CLOUDSDK_PYTHON environment variable in the install.sh shell script...But nothing is working. Here is the code from that file:
echo Welcome to the Google Cloud SDK!
if [ -z "$CLOUDSDK_PYTHON" ]; then
if [ -z "$(which python)" ]; then
echo
echo "To use the Google Cloud SDK, you must have Python installed and on your PATH."
echo "As an alternative, you may also set the CLOUDSDK_PYTHON environment variable"
echo "to the location of your Python executable."
exit 1
fi
CLOUDSDK_PYTHON="python"
fi
I have tried python2.7, and the path to the executable, C:\Python27, but I'm getting this error when I try to run the script with those variables:
install.sh: line 128: $'python\r': command not found
I found this stack question, but none of the solutions worked for me. Any help would be great appreciated.
I had the same issue when the sdk was pointing to the virtualenv python. I solved it by using the default python2.7 in Ubuntu
Type this in termimal
export CLOUDSDK_PYTHON=/usr/bin/python
This is because the gcloud.bat command can't find the right python.exe. I solved the problem by simply put
SET CLOUDSDK_PYTHON=pathWherePythonexeLocate
into the file cloud_env.bat in google Cloud SDK file folder.
And revise the install.sh won't help, because it do nothing to the env since the install.sh was run when you first install gcloud sdk.
and sdk only support python2.7, so the path is pointed to python2.7, such as C:\myname\soft\python27.exe
Two configurations fixed my issue with this.
My laptop runs Windows 10, and I found that there was file:
C:\Users\<myusername>\AppData\Local\Microsoft\WindowsApps\python.exe
That file is size 0 Kb. This directory was ahead of the C:\Python27 path where Python was actually installed. I tried moving C:\Python27 higher in the Path string, but this did not work.
While I did not reboot, I did open a fresh CMD window and confirmed that C:\Python27 was higher in the path than the AppData directory. Still did not work.
When I changed the CLOUDSDK_PYTHON path, just the "path" is not enough. The FULL path must be provided, including the executable name.
Making these two changes enabled gCloud to work.
Of course just as I finished typing the above, I saw email from Google regarding the change below.
IMPORTANT NOTE Python 2.7 will no longer get updates after Jan 1, 2020, so gcloud as of v274.0.0 will run with Python 3x. I can't find a web page announcing this, but there is mention of the change on this page: https://cloud.google.com/sdk/docs/quickstart-linux
The way I solved this was simply by downloading the Versioned SDK instead of the Interactive SDK. I manually added gcloud to my path, and all worked. I still don't know why the interactive download was not finding Python from my systems path, but the Versioned SDK without Python worked.
Thanks for the tips #DanCornilescu.
if you are facing the issue on 274.0.0 on Windows,
This is being tracked in the public bug https://issuetracker.google.com/issues/146458519
An employee replied:
We have a patch for two files that are causing these problems. These
apply in two cases (both on Windows):
1. A new install fails, or
2. You are unable to run gcloud after performing a components update.
For case # 1, please download the attached file install.bat, and copy
it to the location where you have attempted to install gcloud, e.g.
C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk. Then run
it, e.g.
> cd C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk
> .\install.bat
For both cases #1 and #2, download the attached file gcloud.cmd, and
copy it to the bin directory under your gcloud installation, e.g.
C:\Program Files (x86)\Google\Cloud SDK\google-cloud-sdk\bin. When
prompted to replace the previous copy, type Yes. This should allow
you to run gcloud without being prompted to set CLOUDSDK_PYTHON.
The files are attached in the public bug tracker.
Add CLOUDSDK_PYTHON in your system variable and assign it the value of your python.exe file as shown below :
Restart your services so that the change can take effect.
I'm trying to setup regular backups of rethinkdb, but keep running into issues. How do you setup rethinkdb-dump to run from cron?
Here is my script:
$ cat backup.sh
#!/bin/bash
NOW=$(date +"%Y-%m-%d-%H-%M")
/usr/bin/rethinkdb dump -e my_db -f /root/db_backup/$NOW.tar.gz
The script runs just fine when I run it manually. However, when try and run it from cron it doesn't work and I get the following at stderr:
Error when launching 'rethinkdb-dump': No such file or directory
The rethinkdb-dump command depends on the RethinkDB Python driver, which must be installed.
If the Python driver is already installed, make sure that the PATH environment variable
includes the location of the backup scripts, and that the current user has permission to
access and run the scripts.
Instructions for installing the RethinkDB Python driver are available here:
http://www.rethinkdb.com/docs/install-drivers/python/
It appears to be a Python environment issue, but I cannot figure out how to make it happy... thoughts? Help!
When you run it from that backup.sh script, it maybe run without correct PATH setup and cannot found the PATH of rethinkdb-dump.
First, let find out where is rethinkdb-dump
which rethinkdb-dump
(on my pc, I guess it's very different on your pc)
/usr/local/bin/rethinkdb-dump
Now, try to append the PATH to your script backup.sh
#!/bin/bash
export PATH="$PATH:/path/to/folder/contain-rethinkdb-dump"
# The rest of your script normally
So take my example, I will put it like this:
export PATH="$PATH:/usr/local/bin"
I think your rethinkdb-dump live outside normal bin folder (/usr/bin, /usr/local/bin etc)
The python installer for windows installs scripts and packages in subfolders here:
$env:APPDATA\Python\Python37 for powershell
%APPDATA%\Python\Python37 for cmd
cd this directory to see /Scripts and /site-packages (pip packages)
ERROR: Python 3 is not supported by the Google Cloud SDK. Please use a Python 2.x version that is 2.6 or greater.
If you have a compatible Python interpreter installed, you can use it by setting the CLOUDSDK_PYTHON environment variable to point to it.
I guess the first question we should be asking is "with all the money google makes off of their customers why can't they hire someone to ensure that their cloud sdk works with python 3?"
How exactly to overcome this error on linux? What specific files need to be edited? and where should those files be located?
I searched around, a lot, and found this question about how to fix this on Windows, but the answer is not really that comprehensive.
Thus far I've attempted:
One source of documentationsays to modify a file called app.yaml, but I searched using the command find . -name "app.yaml" and no such file exists.
Specifically I'm using arch linux, I originally tried to use the AUR package but it's disfunctional.
So I installed from the documentation, making sure to edit the ./install.sh file, specifying python2 as per this discussion on the google groups, that doesn't work either. after running the command gcloud auth login I get the same error as posted above.
This is a very easy thing to solve. The native python command on the Arch command line is actually for Python 3. The SDK requires Python2.7 and the
Just go to the google-cloud-sdk folder and open the install.sh file.
Change the CLOUDSDK_PYTHON="python" value to CLOUDSDK_PYTHON="python2.7"
Rerun the install with the command ./install.sh in the same folder and follow the prompts.
That's all.
I had the same issue so I did a little change in the dev_appserver.py. This file is in the following path :
google-cloud-sdk/bin
change the shebang from /usr/bin/env python to /usr/bin/env python2
I see this almost every time I update gcloud SDK, especially when running dev_appserver.py <my app config yaml file>
I found that setting the CLOUDSDK_PYTHON env variable to 'python2' seems to silence the error. E.g on macOS:
export CLOUDSDK_PYTHON=python2
Not sure why they simply cannot make this dev server compatible with Python 3 already