Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio Code's debugger & pipenv

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

like image 918
Kazuaki Suzuki Avatar asked Jan 25 '20 07:01

Kazuaki Suzuki


People also ask

Does Visual Studio code have a debugger?

One of the key features of Visual Studio Code is its great debugging support. VS Code's built-in debugger helps accelerate your edit, compile, and debug loop.

How do I Debug in VS Code?

Getting Started. Open a file to debug (either package main source file or the test file) in the editor, and select the Run and Debug button from the Run view. Alternatively, you can start debugging using Start Debugging (F5) command from the Run menu or from the Command Palette (Linux/Windows: Ctrl+Shift+P, Mac: ⇧+⌘+P) ...

How do I enable debugging in Visual Studio?

In the Visual Studio toolbar, make sure the configuration is set to Debug. To start debugging, select the profile name in the toolbar, such as <project profile name>, IIS Express, or <IIS profile name> in the toolbar, select Start Debugging from the Debug menu, or press F5.


1 Answers

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.

like image 82
Brett Cannon Avatar answered Sep 20 '22 05:09

Brett Cannon