visual studio – VS Code – Python – ModuleNotFoundError


I am running into problems when executing python modules from within a virtual environment using Python and am praying that someone out there is able to help!

The issue I appear to be having is that VS Code is not looking for any libraries installed within my .venv directory path, as the python module runs without errors when run using the cmd within the Terminal window.

I think there may be an issue with my vs code settings.

Here are the steps I have already tried:

  1. uninstalled and reinstalled both Python and VS Code
  2. created a new virtual environment and selected the Python Interpreter for the .venv virtual environment.
  3. activated the .venv environment using cmd in my terminal
  4. installed the pandas module (also uninstalled and re-installed multiple times)
  5. confirmed pandas is available within .venv
  6. Executed my code successfully in the Terminal window using cmd

I have Python 3.12.3 installed and the latest version of VS Code, on my Windows machine.

I have created a test.py file which has some basic code for testing which includes sys.path information, which is shown below:

def main():
  myvar = "hello"
  print(myvar)
  return

main()


import sys
print(sys.path)

import pandas as pd
print(pd.__version__)

I have include the output I get when running the python script in VS Code (after selecting the .venv python interpreter below:

[Running] python -u "c:\Users\hemal\my_projects\test.py"
hello
['c:\\Users\\hemal\\my_projects', 'C:\\Users\\hemal\\AppData\\Local\\Programs\\Python\\Python312\\python312.zip', 'C:\\Users\\hemal\\AppData\\Local\\Programs\\Python\\Python312\\DLLs', 'C:\\Users\\hemal\\AppData\\Local\\Programs\\Python\\Python312\\Lib', 'C:\\Users\\hemal\\AppData\\Local\\Programs\\Python\\Python312', 'C:\\Users\\hemal\\AppData\\Local\\Programs\\Python\\Python312\\Lib\\site-packages']
Traceback (most recent call last):
  File "c:\Users\hemal\my_projects\test.py", line 12, in <module>
    import pandas as pd
ModuleNotFoundError: No module named 'pandas'

[Done] exited with code=1 in 0.154 seconds

The output using cmd from my terminal window is shown below:

(.venv) C:\Users\hemal\my_projects>python test.py   
hello
['C:\\Users\\hemal\\my_projects', 'C:\\Users\\hemal\\AppData\\Local\\Programs\\Python\\Python312\\python312.zip', 'C:\\Users\\hemal\\AppData\\Local\\Programs\\Python\\Python312\\DLLs', 'C:\\Users\\hemal\\AppData\\Local\\Programs\\Python\\Python312\\Lib', 'C:\\Users\\hemal\\AppData\\Local\\Programs\\Python\\Python312', 'C:\\Users\\hemal\\my_projects\\.venv', 'C:\\Users\\hemal\\my_projects\\.venv\\Lib\\site-packages']
2.2.2

(.venv) C:\Users\hemal\my_projects>

Looking over the output from both methods of running the code, it appears that when I run my test.py module using VS Code, it is not pointing to the .venv environment.

Here is a list of the libraries installed on my virtual environment, taken using pip freeze:


(.venv) C:\Users\hemal\my_projects>pip freeze
cachetools==5.3.3
certifi==2024.2.2
charset-normalizer==3.3.2
google-auth==2.29.0
google-auth-oauthlib==1.2.0
gspread==6.1.2
idna==3.7
numpy==1.26.4
oauthlib==3.2.2
pandas==2.2.2
pyasn1==0.6.0
pyasn1_modules==0.4.0
python-dateutil==2.9.0.post0
pytz==2024.1
requests==2.32.2
requests-oauthlib==2.0.0
rsa==4.9
six==1.16.0
tzdata==2024.1
urllib3==2.2.1

(.venv) C:\Users\hemal\my_projects>

This is the first time posting on here, so I hope the above is legible and makes sense?!?

Any help would be very much appreciated.

Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *