Home > Back-end >  Can't debug or execute python code inside venv from vscode directly (No module named...)
Can't debug or execute python code inside venv from vscode directly (No module named...)

Time:11-09

I've been trying to run my python code inside a virtual environment (on windows) for a couple of weeks. I gave up for a while but now I want to debug and I can't get it to work. The problem is that my python scripts (in a virtual environment) use certain modules that have been installed in the environment, in fact, I can run the code from a cmd terminal without problems. However, when I run it from vscode directly or from another type of terminal (powershell, for example).
I get the error that it does not find the modules. I thought that changing the default terminal in vscode might help but it doesn't.

I attach the error:

    (venv) PS C:\Users\sydea\Desktop\Cargadores solares\Proyecto\OCPP-1.6J-CS-I> python .\test_server.py
Traceback (most recent call last):
  File "C:\Users\sydea\Desktop\Cargadores solares\Proyecto\OCPP-1.6J-CS-I\test_server.py", line 2, in <module>
    from aioconsole import ainput
ModuleNotFoundError: No module named 'aioconsole'

Also, in the lower part of vscode where the selected interpreter is shown, the appropriate interpreter appears. I attach a picture:

My vscode view

Also I attach the list of pip packages installed in the virtual environment: modules installed in venv

Thanks for your time!

CodePudding user response:

I had a look at the pic you attached for modules installed in venv. I notice that in the warning, the venv directory is not the one from your workspace.

As such, I suspect the issue is right there. Open the command pallete (ctrl shift P), type interpreter, and click on the option Python: Select Interpreter. Now type the path to the venv ".\venv\scripts\python.exe".

One done, open a new terminal and type .venv\scripts\activate. This should now activate the right venv. Once here, You should reinstall the required libraries if they are not there.

After that, it should work.

  • Related