Bikram budhathoki
Bikram budhathoki

Reputation: 11

Imported module not found

I am writing a python script which requires pywin module and I have already installed them but the vscode is showing "Import "pywin32" or "pywin32com" could not be resolved". What am I missing here?

# import win32com.client
import pywin32
import pywin32com
# Open a Word application instance
word = pywin32com.client.Dispatch("Word.Application")

# Open the desired document in Word
doc = word.Documents.Open("D:/files/test.doc")

# Loop through all paragraphs in the document
for paragraph in doc.Paragraphs:
  # Check if the current paragraph is selected
 if paragraph.Range.Information(pywin32com.client.constants.wdActiveEndAdjustedPageNumber) > 0:
    # Print the selected paragraph's text
    print(paragraph.Range.Text)

The above code is the code I am trying to execute. The pip list in showing the following list:

numpy             1.23.5      
pandas-stubs      1.5.2.221124
Pillow            9.3.0       
pip               22.3.1      
PyPDF2            2.12.1
pypiwin32         223
pytz              2022.6
pywin32           305
setuptools        65.6.3
six               1.16.0
types-pytz        2022.6.0.1
typing_extensions 4.4.0
urllib3           1.26.13
wheel             0.38.4

Here it is showing that pywin32 is already installed but the import error is not resolved. Why? Any advice on the problem would be greatly appreciated. Thanks in advance!!

Upvotes: 0

Views: 382

Answers (1)

Nora
Nora

Reputation: 161

Make sure that the python version you use for building the script is the same as the python version you use to run pip install pywin32.

When you run pip install ... it will show the part to the python version which is being installed, use it to compare with the python version which show when you build/debug the script.

Example of logging when installing a library (it has python310 -> it is being installed on Python 3.10)

Requirement already satisfied: loguru in c:\users\ntgr\appdata\roaming\python\python310\site-packages (0.6.0)

Example of logging when running a script (it has Python310 -> it is running on Python 3.10):

PS D:\_PROJECT\bot_hunter_5.0\hunter-5-0-bot>  & 'C:\Python310\python.exe' 'c:\Users\NTGR\.vscode\extensions\ms-python.python-2022.18.2\pythonFiles\lib\python\debugpy\adapter/../..\debugp
y\launcher' '56421' '--' 'd:\_PROJECT\bot_hunter_5.0\hunter-5-0-bot\bot.py

'

Upvotes: 1

Related Questions