I am connecting to an HPC environment through VScode remote ssh and would like to run python code directly in VScode for testing purposes. I would like to set the python interpreter to a singularity container which runs python upon execution. This was done by adding the following lines in the .def file of the container:
%runscript
exec python
Executing the container manually does start a python session as intended. However, nothing happens when setting the path of the python interpreter to the container file in VScode. It keeps asking for the path of the interpreter as if it did not receive any input. I tried to set the path both in VScode GUI and by setting the default path in the JSON settings file like so:
{
"python.defaultInterpreterPath":"~/path/to/singularity.sif"
}
Although this approach was reported successful here:
Python code completion IntelliSense using Singularity container interpreter ;
and there:
How can I use a python interpreter in a singularity/docker image in visual studio code .
I can however select interpreters that are not contained in singularity containers and it works fine. Notably, it works if I build the singularity container as a sandbox and provide a path to the python's bin in the sandbox.
Any idea what could go wrong here? I am using the latest version of VScode (v1.68.1) with the the Remote - SSH extension (v0.82.1) and Python extension (v2022.8.0) on Ubuntu 22.04; singularity images were created with (v3.5.3).
I have been trying to solve the same issue but found an alternative solution.
So instead of running vscode server on the host, you can actually run vscode server inside the singularity container on the host. The following procedure is cut from this comment by #oschulz github user.
Make sure you have vscode version >= v1.64.
Put "remote.SSH.enableRemoteCommand": true in your vscode's settings.json
In your local machine, add something like below to $HOME/.ssh/config
Host myimage1~*
RemoteCommand singularity shell /path/to/image1.sif
RequestTTY yes
Host somehost myimage1~somehost
HostName some.host.somewhere
User your_username_
Test that it's working: try ssh myimage1~somehost and something like python3 --version
Add this to your settings.json:
"remote.SSH.serverInstallPath": {
"myimage1~somehost": "~/.vscode-container/myimage1",
}
In vscode, connect to host using RemoteSSH, specify hostname as myimage1~somehost.
You might want to Kill VS Code Server On Host... if something is not working. Just type "kill" in vscode's command palete.
My Flask app environment is using anaconda
When I use IDE like PyCharm, it provides a terminal which already in the specific conda environment, like this:
It could easily let me to start the app via command flask run
Then my question is, how could I start the app in an original terminal?
I tried to use flask run in the original terminal but the dependency module are not found (All dependencies are placed in the conda environment actually)
You can use conda run for this. E.g.,
conda run -n flask_env_3 flask run
You may need to add the flag(s) --live-stream or --no-capture-output (or both), for interactivity.
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 using python 2.7 with flask and using pycharm professional IDE, I am running the flask application using a virtual environment from inside pycharm.
When I open a terminal inside pycharm and use CLI commands, it works, and when I open a terminal (regular terminal) outside of the IDE, and trying to use the same command it's not working, the app is still running all the time, and the command is excactly the same.
When I try to activate the same venv outside of the IDE i get permission issue, I assume that it has to do with the venv already being active inside the IDE.
What is the issue? I need to run the same virtual environment in order to use the CLI commands?
How can i access the CLI commands from outside of the IDE?
Thanks
When I try to activate the same venv outside of the IDE I get permission issue
Most likely the problem is all about permission access to the virtual environment's files. Check out access permissions and user:group ownership using ls -al (if you're on Mac or Linux), more info here:
https://linux.die.net/man/1/ls
https://linux.die.net/man/1/chmod
https://linux.die.net/man/1/chown
I assume that it has to do with the venv already being active inside the IDE
Definitely not, you can activate it as many times as you want.
I need to run the same virtual environment in order to use the CLI commands?
At least you have to have all the dependencies installed in your other environment (global or virtual) if you have plans to use one.
when I open a terminal (regular terminal) outside of the IDE, and trying to use the same command it's not working
You'd better post a full error output so that we could check the actual error. Also what command are you trying to run?
I often do this to prepare for some django debugging:
Open up a terminal window in os x (10.6)
start the python interpreter
run these commands in python:
from django.core.management import setup_environ
import settings
setup_environ(settings)
Is it possible to automate these actions and make a shortcut that I can doubleclick to invoke?
If you use django you should consider running the command python manage.py shell to debug django applications.
Even better, try the shell_plus command from the excellent django command extension add-on.
You can use the PYTHONSTARTUP environment variable to your advantage here if you want to throw that in a script and then have a batch file load up a shell with that environment variable.