Visual Studio Code's debugger & pipenv

Question:

I would like to use Visual Studio Code’s debugger to debug my python code, but exception occurs.
I use Windows 10, WSL, Debian, Python 3.7.6.

Exception has occurred: ModuleNotFoundError
No module named 'flask'
  File "/home/kazu/test/main.py", line 2, in <module>
    from flask import Flask

ModuleNotFound

This is python debugger console’s record.

pyenv shell 3.7.6
/home/kazu/.pyenv/versions/3.7.6/bin/python /home/kazu/.vscode-server/extensions/ms-python.python-2020.1.58038/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 52440 /home/kazu/test/main.py 
kazu@D:~/test$ pyenv shell 3.7.6
kazu@D~/test$ /home/kazu/.pyenv/versions/3.7.6/bin/python /home/kazu/.vscode-server/extensions/ms-python.python-2020.1.58038/pythonFiles/ptvsd_launcher.py --default --client --host localhost --port 52440 /home/kazu/test/main.py 

However, I have already installed flask using pipenv. When I don’t use debugger, there isn’t module error.

This is my main.py

from __future__ import unicode_literals
from flask import Flask
from flask import render_template
from flask import request
from flask import send_file
import os
import youtube_dl

app = Flask(__name__)

@app.route("/", methods=['POST', 'GET'])
def index():
    if request.method == "POST":
        if os.path.exists("/tmp/output.mp4"):
            os.remove("/tmp/output.mp4")
        else:
            print("Can not delete the file as it doesn't exists")
        url = request.form['url']
        ydl_opts = {'outtmpl': '/tmp/output.mp4', 'format':'bestvideo[ext=mp4]'}
        with youtube_dl.YoutubeDL(ydl_opts) as ydl:
            ydl.download([url])

        return send_file("/tmp/output.mp4",as_attachment=True)
    else:
        return render_template("index.html")

if __name__ == "__main__":
    app.run()

I searched the Internet and found that I should put my .venv folder in the project directory. So, I operated this command.

export PIPENV_VENV_IN_PROJECT=1

and now my directory structure is this.

.
├── main.py
├── Pipfile
├── Pipfile.lock
├── .venv
└── templates
    └── index.html

However, I get same error message.

Then, I searched the Internet again and this time I set vs code’s python venv path, but I got the same error message.

python_venv
Could you give me any information or suggestion?

Thank you in advance.

Sincerely, Kazu

Asked By: Kazuaki Suzuki

||

Answers:

Sometimes the Problem is not even the Import.
If you have a syntax error on a file that is loaded before the same Exception is Displayed.

Answered By: BlumCore

If you look in the bottom-left corner of your screen you will notice you are currently running against a pyenv install of Python and not a pipenv virtual environment. If you click on the interpreter name and select the appropriate environment where you installed flask it should fix your issue.

Answered By: Brett Cannon

In your debbug config file launch.json add "console": "integratedTerminal". It will first try to run virtual envs like pypenv shell and then run the server.

Answered By: Deskom88