'No module named flask' in VSCode even when I have installed flask - python

I have my Flask installed in my main directory for playing with Flask projects and I don't want to change it (its name is tryFlask).
My project is in one of the subdirectories to tryFlask (main_project).
How should I config my launch.json so that it doesn't throw errors when I try to debug?
Current config which throws:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Flask",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "my_app.py",
"FLASK_ENV": "tryFlask",
"FLASK_DEBUG": "1"
},
"args": [
"run",
"--no-debugger",
"--no-reload"
],
"jinja": true,
"justMyCode": true
}
]
}
And yes I have already checked "FLASK_APP": "my_project/my_app.py". Doesn't work. Neither does reinstalling dependencies or even choosing different interpreters from venv's bin subdirectory.

If you want to use flask with vscode, following this tutorial is a good start.
According to the question in your post, if you want to specify the python interpreter when debugging, you can add the python configuration to launch.json
Simple example:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
// Modify the python interpreter with flask installed on your machine
"python": "C:\\WorkSpace\\pytest10\\.venv\\Scripts\\python.exe"
// "python": "${workspaceFolder}/.venv/Scripts/python.exe"
}
]
}
For more launch.json configuration, you can check this link.

Related

Problem in debugging django project using VS code

I have a problem while debugging a django project using VS code, the problem that nothing happened when I click to debug button, I can launch my script just by tapping in terminal python manage.py runserver.
Here is my launch.json file, and note please that I tried a lot of examples, and still the same problem:
{
"version": "0.2.0",
"configurations": [
{
"name": "Django",
"python": "C:/Users/msekmani/Desktop/dashboard_project/venv/Scripts/python.exe",
"type": "python",
"request": "launch",
"program": "C:/Users/msekmani/Desktop/dashboard_project/IPv2/src/manage.py",
"console": "internalConsole",
"args": ["runserver"],
"django": true,
"justMyCode": true,
},
]
}
I am using python version 3.6 and for the OS is Windows.
Note please that I also tried to creat a Python debug and also it's not working also, and here is my launch.json script:
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true
}
]
}
I find a solution to solve this problem by upgrade the python version to 3.7, I don't have any idea about the problem happened in version 3.6, by the way, the upgrade python version is the solution.

Time out waiting for launcher to connect in VS code

I did python debugging in VS code.
The following is the launch.json file:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"python": "${command:python.interpreterPath}",
"program": "${file}",
"cwd": "${workspaceFolder}",
"env": {},
"envFile": "${workspaceFolder}/.env",
"debugOptions":[
"RedirectOutput"
],
"console": "integratedTerminal"
}
]
}
The following is settings.json file:
{
"python.pythonPath": "c:\\Users\\susan\\Documents\\PythonScripts\\venv\\Scripts\\python.exe",
// to fix 'Timeout waiting for debugger connections'
"python.terminal.activateEnvironment" : false
}
When I debug the python script in VS code, I got Time out waiting for launcher to connect and cannot debug the python script.
May I know how can I solve this issue?
Its very simple. Open the launch.json file and add the following into it:
{
"name": "Python: Debug Console",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "internalConsole"
}
Then save and exit it. Whatever you do, DO NOT clear the text already in there or else it may make it worser
If for whatever reason "internalConsole" isn't a good solution:
in your shell script:
export PROCESS_SPAWN_TIMEOUT=30
or just hack the code directly (will be reverted if you update the extension):
.vscode-server/extensions/ms-python.python-2022.18.2/pythonFiles/lib/python/debugpy/adapter/launchers.py[161]:
change:
timeout=(None if sudo else common.PROCESS_SPAWN_TIMEOUT)
to:
timeout=30,

python vscode environment variables not available

I have attempted to follow the directions for environment variables in my project and thought I was doing the correct thing but it does not appear to be working when debugging my python code in vscode.
I have a launch.json for a project in .vscode/
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal",
"justMyCode": true,
"envFile": "${workspaceFolder}/dev.env"
}
]
}
and a dev.env file in my project root
PYTHONUNBUFFERED=1
PASSWORD=abc123
DEBUG_SSO=user.name
CELERY_DEBUG=True
SEND_MAIL=True
when I hit a breakpoint and do "os.getenv('PASSWORD')" it returns None, even though the variable is set in dev.env.
Any idea what is wrong with this configuration?
This may be due to the relative path being unrecognized.
Add this line to the file "launch.json" : "cwd": "${fileDirname}"

VS code python debugger error with print statements

So when ever im trying to debug any Python script on VS code an that script has a print or input statement, it crashes an throws me an "AttributeError" that says 'NoneType' object has no attribute 'write', any ideas why this happens? i cant find any information on Google about it
here is a screenshot in the error:Link to the screenshot
and also here is my configuration file:
{
// Use IntelliSense para saber los atributos posibles.
// Mantenga el puntero para ver las descripciones de los existentes atributos
// Para más información, visite: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"program": "${file}",
"console": "integratedTerminal"
},
{
"name": "Python: Attach",
"type": "python",
"request": "attach",
"port": 5678,
"host": "localhost"
},
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"console": "integratedTerminal",
"args": [
"runserver",
"--noreload",
"--nothreading"
],
"django": true
},
{
"name": "Python: Flask",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "app.py"
},
"args": [
"run",
"--no-debugger",
"--no-reload"
],
"jinja": true
},
{
"name": "Python: Current File (External Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "externalTerminal"
}
]
}
VSCODE working fine for me
a) Install VSCODE or upgrade to version 1.28.1
b) REFRESH Python Extension
If you face any issue like timeout etc read
https://github.com/Microsoft/vscode-python/issues/2410
very carefully
Edit settings.json of python extension
a) Disable
Terminal: Activate Environment
Activate Python Environment in Terminal created using the Extension.
b) enable
Terminal: Execute In File Dir
When executing a file in the terminal, whether to use execute in the file's directory, instead of the current open folder.
c) Remove pythonW and put python in
Python Path
Path to Python, you can use a custom version of Python by modifying this setting to include the full path.
Everything above from
https://github.com/Microsoft/vscode-python/issues/2410
Though a happy ending I can foresee a future of unstable releases
for a wonderful VSCODE and even better Python Extension
An attribute error usually means that whatever object your working with is actually none. This can happen because something happened upstream or downstream of your call.
In the case of your single print statement, the only thing I can think of is perhaps it has something do so with double quotes.. It doesn't really make sense that double quotes would cause this but who knows.
what happens when you try
print('I will crash!!!')
If that still fails then I would say that perhaps vs is trying to write to a file, configuration, log, console or something else and is running into permission issues.
EDIT
After looking closesr at your configuration files, I see you have two that start with
"name": "Python: Current File ....
So I rewrote your config file, it still includes the specific files that were named, and their configs, but I eliminated one of the current file entries and made it basic.
{
// Use IntelliSense para saber los atributos posibles.
// Mantenga el puntero para ver las descripciones de los existentes atributos
// Para más información, visite: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File (External Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "externalTerminal"
},
{
"name": "Python: Attach",
"type": "python",
"request": "attach",
"port": 5678,
"host": "localhost"
},
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"console": "integratedTerminal",
"args": [
"runserver",
"--noreload",
"--nothreading"
],
"django": true
},
{
"name": "Python: Flask",
"type": "python",
"request": "launch",
"module": "flask",
"env": {
"FLASK_APP": "app.py"
},
"args": [
"run",
"--no-debugger",
"--no-reload"
],
"jinja": true
}
]
}
I have it set up to use the external console (standard windows cmd). If you want to use the vs console replace
{
"name": "Python: Current File (External Terminal)",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "externalTerminal"
},
with
{
"name": "Python: Current File (Integrated Terminal)",
"type": "python",
"request": "launch",
"stopOnEntry": true,
"program": "${file}",
"console": "integratedTerminal"
},
Do NOT forget to save a copy of your old config file first. That way if VS freaks out from manually changing this file you can always revert back.
I am looking for the possibility that VS can't decide which terminal to output too, but at the same time you only get this while debugging so....
Now I did see a flag in the config for no debug but it was for a flask app.

How to use visual studio code to debug django

I'm new at django development and come from desktop/mobile app development with Xcode and related IDE.
I have to use Django and I was wondering if there was an efficient way to debug it using Visual Studio Code (or Atom).
Any help related to Django IDE would be helpful too.
For VSCode (full disclosure, I'm one of the VSCode developers) try installing the Python extension to get started.
This documentation covers debugging Django. There should be a included debug configuration or you can add your own to the launch.json file:
{
"name": "Django",
"type": "python",
"request": "launch",
"stopOnEntry": false,
"pythonPath": "${config.python.pythonPath}",
"program": "${workspaceRoot}/manage.py",
"args": [
"runserver",
"--no-color",
"--noreload"
],
"debugOptions": [
"WaitOnAbnormalExit",
"WaitOnNormalExit",
"RedirectOutput",
"DjangoDebugging"
]
}
The Python extension also provide many other features that you may find useful.
VSCode has an official tutorial explaining this:
https://code.visualstudio.com/docs/python/tutorial-django
There are several steps that need to be taken, which I don't all want to write out manually, since there are quite some steps, but I'll try to summarize what needs to be done:
The text below is basically a partial copy of the above tutorial, I am not claiming I came up with this myself.
1. Make sure to check out the prerequisites (use VS Code Python extension, install Python on local machine) link to docs
2. Use Python virtual environment link to docs
Besides using a Python virtual environment, you also need to select the Python executable inside this virtual environment as the interpreter in VS Code. This can be done like so:
In VS Code, open the Command Palette (View > Command Palette or (Ctrl+Shift+P)). Then select the Python: Select Interpreter
Then you select the Python executable inside your virtual environment, which you can recognize by it's path.
3. Create debugger lauch profile
as described here, in the documentation
upper left of the VS Code window)
4. Now you can start debugging
this part of the documentation will give you an introduction on how to do that
Only experimental configuration works for me.
{
"name": "Django",
"type": "pythonExperimental",
"request": "launch",
"program": "${workspaceFolder}/manage.py",
"args": [
"runserver",
"--noreload",
"--nothreading"
],
"django": true
},
Standard config causes Unverified breakpoint issue.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Django",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}\\manage.py",
"args": [
"runserver"
],
"django": true
},
{
"name": "Django: makemigrations",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}\\manage.py",
"args": [
"makemigrations"
],
"django": true
},
{
"name": "Django: migrate",
"type": "python",
"request": "launch",
"program": "${workspaceFolder}\\manage.py",
"args": [
"migrate"
],
"django": true
},
]
}
Nothing worked for me until I had disabled auto reload (--noreload as an argument is crucial, not really sure why it causes problem with debugging)

Categories