[Python] No module named 'pywintypes'; seemingly problem with virtual environment

Discussão em 'Python' iniciado por Stack, Março 1, 2025.

  1. Stack

    Stack Membro Participativo

    I've read through all the relevant posts to this question, but sadly most remain unanswered, or don't seem to apply to my specific situation.

    I want to use the win32com module, which is part of the pywin32 package. When I import win32com, I get the error:


    ModuleNotFoundError: No module named 'pywintypes'
    ​

    When I try to pip install pywin32 (which should contain pywintypes) using the terminal with the virtual environment activated, I get the message:


    Requirement already satisfied: pywin32 in c:\users\username\appdata\local\packages\pythonsoftwarefoundation.python.3.12_qbz5n2kfra8p0\localcache\local-packages\python312\site-packages (308)
    ​

    When I want to uninstall the pywin32 package in the virtual environment in order to reinstall it, I get the message:


    Found existing installation: pywin32 308 Not uninstalling pywin32 at c:\users\username\appdata\local\packages\pythonsoftwarefoundation.python.3.12_qbz5n2kfra8p0\localcache\local-packages\python312\site-packages, outside environment C:\Users\username\Documents\Python_Projects\ALP.venv Can't uninstall 'pywin32'. No files were found to uninstall.
    ​

    So it seems that when (un)installing pywin32, pip looks at localcache\site-packages. Yet the venv can't access the packages installed there. I want to install pywin32 in the venv, but that is not possible since pip already finds an installation. It's just that it looks for it in localcache\site-packages instead of venv\Lib\site-packages.

    When doing pip list inside the virtual environment, pywin32 (version 308) shows up. Also when checking python -c "import sys; print(sys.executable)", the correct python installation shows up (the one in the venv).

    I've tried updating the PATH variable with a path to venv\Scripts. I've also copied the pywin32 folders from localcache\site-packages to venv\Lib\site-packages, with no effect.

    Something that's odd, though I'm no sure if it's problematic, is that if I do where python in the venv, I get two paths. One to the python installation in the venv, and one to a general installation in Windowsapps. Could this be what is confusing pip?

    Any help would be much appreciated.

    Continue reading...

Compartilhe esta Página