Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't find pywintypes in virtual environment #2308

Open
nhunter77018 opened this issue Jun 30, 2024 · 2 comments
Open

Can't find pywintypes in virtual environment #2308

nhunter77018 opened this issue Jun 30, 2024 · 2 comments

Comments

@nhunter77018
Copy link

Steps to reproduce the problem

Simple program to read system keywords from a jpg file.

import pythoncom
from win32com.propsys import propsys
from win32com.shell import shellcon
import sys

def readTags(imgf_path):

    # get PROPERTYKEY for "System.Keywords"
    pk = propsys.PSGetPropertyKeyFromName("System.Keywords")

    # get property store for a given shell item (here a file)
    ps = propsys.SHGetPropertyStoreFromParsingName(imgf_path, None, shellcon.GPS_READWRITE, propsys.IID_IPropertyStore)

    # read & print existing (or not) property value, System.Keywords type is an array of string
    keywords = ps.GetValue(pk).GetValue()

    return keywords

if __name__ == '__main__':
        print(readTags(sys.argv[1]))

Works great!

C:\Users\user\source\repos\Test>python test.py "D:\FaceDb\Friends\images\friends (1).jpg"
['Friends', 'Matt LeBlanc', 'Jennifer Aniston']

Create & activate virtual environment.

C:\Users\user\source\repos\Test>python -m venv .\env

C:\Users\user\source\repos\Test>env\Scripts\activate.bat

Fails.

(env) C:\Users\user\source\repos\Test>python test.py "D:\FaceDb\Friends\images\friends (1).jpg"
Traceback (most recent call last):
File "C:\Users\user\source\repos\Test\test.py", line 1, in
import pythoncom
File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\pythoncom.py", line 2, in
import pywintypes
ModuleNotFoundError: No module named 'pywintypes'

Deactivate virtual environment, all is good.

(env) C:\Users\user\source\repos\Test>env\Scripts\deactivate.bat
C:\Users\user\source\repos\Test>python test.py "D:\FaceDb\Friends\images\friends (1).jpg"
['Friends', 'Matt LeBlanc', 'Jennifer Aniston']

Python version and distribution:

Python 3.11.8

pywin32 version:

306?
C:\Users\user\source\repos\Test>pip3 install pywin32
Requirement already satisfied: pywin32 in c:\users\user\appdata\local\programs\python\python311\lib\site-packages (306)

Installed from PyPI or exe installer:

PyPI

Windows Version:

Windows 10 Home Version 22H2

DLL locations:

C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\pywin32_system32\pywintypes311.dll
C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\pywin32_system32\pythoncom311.dll

@Avasam
Copy link
Collaborator

Avasam commented Jun 30, 2024

Did you install pywin32 in your virtual environment and are you sure that when calling python it's using the executable in your venv?
I'm asking because

C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\pythoncom.py
c:\users\user\appdata\local\programs\python\python311\lib\site-packages
C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\pywin32_system32\pywintypes311.dll
C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\site-packages\pywin32_system32\pythoncom311.dll

Don't look like paths to your ./env venv

@nhunter77018
Copy link
Author

nhunter77018 commented Jun 30, 2024

Hi, Avasam, thanks for your speedy response.

You are correct. c:\users\user\appdata\local\programs\python\python311 is my python home folder.

When I couldn't get it to work, I tried installing pywin32 in my virtual environment using pip like this.

(env) pip3 install pywin32
Requirement already satisfied: pywin32 in c:\users\user\appdata\local\programs\python\python311\lib\site-packages (306)

(env) C:\Users\user\source\repos\Test>python -c "import pywintypes,pythoncom;print(pywintypes.file,pythoncom.file,sep='\n')"
Traceback (most recent call last):
File "", line 1, in
ModuleNotFoundError: No module named 'pywintypes'

What is it I need to do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants