0

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!

4
  • 1
    Try checking if that package is installed in the virtual environment. You may use python -m pip list Commented Nov 8, 2021 at 11:18
  • @kiner_shah Hello, thanks for your answer, I guess they are (although dunno how to make completely sure) since I can run the code directly from cmd without any errors, also I can execute python on terminal and import those modules without errors as well. Commented Nov 8, 2021 at 11:20
  • Can you execute them in virtual environment when opening from command line or terminal? Commented Nov 8, 2021 at 11:21
  • @kiner_shah Yes I can execute them in venv from cmd terminal. Also, all the packages I need are shown when using python -m pip list. Commented Nov 8, 2021 at 11:23

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Ohh!! I see now what the problem was. You are right the packages were installed in another venv for some reason (I still don't know how I messed that up), I now installed the packages on the correct venv and now it's working fine. The interpreter was set correctly from the beginning. Thank you so much!!!!
you may consider using the command: path to venv\scripts\python.exe -m pip install [package] to make sure you install your packages to the right location. Note that in the powershell terminal you would have to put any folder name which has a space with quotations like this: d:\'python programs'\venv\scripts\python.exe -m ...
Yep! That is how I did it this time. I will keep this in mind for future venvs creations! Thank you for your time and help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.