ModuleNotFoundError in venv only for psycopg2

  import, psycopg2, python, vscode-settings, windows

I have been working in a Windows 10 system within a venv in VS Code for a few months now, and I have never had issues with the virtual environment finding any packages that I have downloaded previously (e.g., Numpy). However, when I try to run the following code:

from sqlalchemy import create_engine

if __name__ == "__main__":
    # Read in the necessary paths
    engine = create_engine("postgresql+psycopg2://postgres:[email protected]:5432/so2020")

I get the following Traceback:

[Running] python -u "c:Usersdjgracodestackoverflow2020entity_factoryentity_factory.py"
Traceback (most recent call last):
  File "c:Usersdjgracodestackoverflow2020entity_factoryentity_factory.py", line 5, in <module>
    engine = create_engine("postgresql+psycopg2://postgres:[email protected]:5432/so2020")
  File "C:UsersdjgraAppDataLocalProgramsPythonPython36libsite-packagessqlalchemyengine__init__.py", line 456, in create_engine
    return strategy.create(*args, **kwargs)
  File "C:UsersdjgraAppDataLocalProgramsPythonPython36libsite-packagessqlalchemyenginestrategies.py", line 87, in create
    dbapi = dialect_cls.dbapi(**dbapi_args)
  File "C:UsersdjgraAppDataLocalProgramsPythonPython36libsite-packagessqlalchemydialectspostgresqlpsycopg2.py", line 737, in dbapi
    import psycopg2
ModuleNotFoundError: No module named 'psycopg2'

I am a little confused here because I have just never had any issues with the venv finding the scripts that I install, but now it seems like my code is searching in the wrong spot (based on the Traceback) and I have no made any changes to my settings (that I can recall). I am trying to get the engine created, but first I need to reconfigure a setting somewhere so that my virutal environment is importing the scripts correctly. I set up VS Code quite a while ago, and I cannot remember if there is a specific setting or not that would need to be modified so that the scripts are being imported properly.

Which setting(s) in VS Code are responsible for getting the correct imports from the Scripts folder of the venv?

Source: Python Questions

LEAVE A COMMENT