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

pywintypes module not found in virtualenv #1455

Closed
eranzim opened this issue Dec 22, 2019 · 21 comments
Closed

pywintypes module not found in virtualenv #1455

eranzim opened this issue Dec 22, 2019 · 21 comments

Comments

@eranzim
Copy link

eranzim commented Dec 22, 2019

Hi!
I pip-installed pywin32 on my machine, and everything works fine. I tried it in a virtualenv, and I get this error when importing pywintypes:
ModuleNotFoundError: No module named 'pywintypes'

I should also note that pip show pywin32 shows it is installed (also inside the virtualenv), and the dll does exist inside ...\myvenv\Lib\site-packages\pywin32-227-py3.8-win-amd64.egg\pywin32_system32\pywintypes38.dll

Python 3.8.0 64-bit on Windows 10 x64
pywin32 227
virtualenv 16.7.9

Possibly related: #1327 (though it seems a different issue to me)

Thanks!!

@mhammond
Copy link
Owner

mhammond commented Jan 6, 2020

That's odd. Note that there should be a pywintypes.py in the win32\Lib directory.

@eranzim
Copy link
Author

eranzim commented Jan 6, 2020

Some more info:

  1. pywintypes.py exists in C:\Python38\Lib\site-packages\win32\lib\pywintypes.py and in C:\...\myvenv\Lib\site-packages\pywin32-227-py3.8-win-amd64.egg\win32\lib\pywintypes.py.
  2. A python shell finds pywintypes's dll:
>>> import pywintypes
>>> pywintypes
<module 'pywintypes' (C:\Python38\Lib\site-packages\pywin32_system32\pywintypes38.dll)>
  1. pywintypes38.dll exists in C:\Python38\Lib\site-packages\pywin32_system32\pywintypes38.dll and in C:\...\myvenv\Lib\site-packages\pywin32-227-py3.8-win-amd64.egg\pywin32_system32\pywintypes38.dll
  2. Python shell's sys.path:
['', 'C:\\Python38\\python38.zip', 'C:\\Python38\\DLLs', 'C:\\Python38\\lib', 'C:\\Python38', 'C:\\Python38\\lib\\site-packages', 'c:\\code\\xxx', 'C:\\Python38\\lib\\site-packages\\pypiwin32-223-py3.8.egg', 'C:\\Python38\\lib\\site-packages\\win32', 'C:\\Python38\\lib\\site-packages\\win32\\lib', 'C:\\Python38\\lib\\site-packages\\Pythonwin']
  1. sys.path inside virtualenv:
['', 'c:\\...\\myvenv\\Scripts\\python38.zip', 'c:\\...\\myvenv\\DLLs', 'c:\\...\\myvenv\\lib', 'c:\\...\\myvenv\\Scripts', 'c:\\python38\\Lib', 'c:\\python38\\DLLs', 'c:\\...\\myvenv', 'c:\\...\\myvenv\\lib\\site-packages', 'c:\\code\\xxx', 'c:\\...\\myvenv\\lib\\site-packages\\netifaces-0.10.9-py3.8-win-amd64.egg', 'c:\\...\\myvenv\\lib\\site-packages\\urllib3-1.22-py3.8.egg', 'c:\\...\\myvenv\\lib\\site-packages\\jsonpickle-1.2-py3.8.egg', 'c:\\...\\myvenv\\lib\\site-packages\\yaxl-0.0.17-py3.8.egg', 'c:\\...\\myvenv\\lib\\site-packages\\pefile-2019.4.18-py3.8.egg', 'c:\\...\\myvenv\\lib\\site-packages\\plumbum-1.6.8-py3.8.egg', 'c:\\...\\myvenv\\lib\\site-packages\\pyyaml-5.3b1-py3.8-win-amd64.egg', 'c:\\...\\myvenv\\lib\\site-packages\\argh-0.26.2-py3.8.egg', 'c:\\...\\myvenv\\lib\\site-packages\\decorator-4.4.1-py3.8.egg', 'c:\\...\\myvenv\\lib\\site-packages\\construct-2.9.45-py3.8.egg', 'c:\\...\\myvenv\\lib\\site-packages\\pathlib2-2.3.5-py3.8.egg', 'c:\\...\\myvenv\\lib\\site-packages\\psutil-5.6.7-py3.8-win-amd64.egg', 'c:\\...\\myvenv\\lib\\site-packages\\parameterized-0.7.1-py3.8.egg', 'c:\\...\\myvenv\\lib\\site-packages\\pywin32-227-py3.8-win-amd64.egg', 'c:\\...\\myvenv\\lib\\site-packages\\future-0.18.2-py3.8.egg']

@colin-adams
Copy link

colin-adams commented Mar 23, 2020

Is this still open? I have the same issue - within a virtualenv, it can't find the module. After deactivation, it can. (with python 3.6)

@colin-adams
Copy link

pip uninstall

followed by

pip install

(in the virtual environment) does the trick for me

@CristiFati
Copy link
Contributor

Could you post the steps to reproduce?

@yuval9313
Copy link

Same issue on a bit more complex situation, I've tried to create a service using pywin32 and encounter this:
image

Step to reproduce:
on global python 38.2
pip install virtualenv
on folder
virtualenv venv
and then

(venv) pip install pywin32
(venv) pip uninstall pywin32
(venv) python -m pip install pywin32

nothing works

@CristiFati
Copy link
Contributor

All who are encountering the error, please post all the details:

  • Python version (and architecture)
  • Virtual environment creation command
  • Any additional commands (activate, pip install, ...)
  • The complete stacktrace

Actually, post all the commands used and their outputs.

For the service question: what does python -c "import win32api" output inside the VEnv?

@eranzim
Copy link
Author

eranzim commented Apr 12, 2020

This is odd - I just created a new virtualenv from scratch, pip installed just pywin32, and it worked. Went back to the previous virtualenv, uninstalled and reinstalled pywin32, and now it works there as well...
So as far as I'm concerned, this issue can be closed (though it seems @yuval9313 still has an issue...)
@colin-adams - thanks :)

@yuval9313
Copy link

yuval9313 commented Apr 12, 2020

For the service question: what does python -c "import win32api" output inside the VEnv?

So this actually outputs blank, Also import pywintypes outputs blank which means that I can import them, other than that it still doesn't work, I actually transferred to a second PC, reinstall python 3.8 and made updated virtualEnv and still gets the same error mentioned in my previous post

@eranzim can you post your code? If I could work with your's it means my issue might be a different one, which means this one could be closed and newly open more specific issue

EDIT: By mistake my entire post was inside the quote 😅

@eranzim
Copy link
Author

eranzim commented Apr 16, 2020

@yuval9313 The code that didn't work was simply import pywintypes from a python console, nothing more. To fix it, pip uninstall and pip install did the trick.

@CristiFati
Copy link
Contributor

CristiFati commented Apr 16, 2020

@yuval9313: What about python -c "import win32api;print(win32api)"? From inside the VEnv (or to be more precise: using the same interpreter launched by the service)?

@yuval9313
Copy link

Once again, on freshly installed env
image
The picture shows that I can actually import this but the error stays the same as the one above.

@CristiFati
Copy link
Contributor

I played very little with PyWin32 services, but I imagine they are started by SYSTEM. Hmm you should write the contents of sys.executable, sys.path, site.getsitepackages(), site.getusersitepackages() in a file, before importing win32api and look at the file after the service fails. Also, do the same thing before by executing the script manually.

@mhammond
Copy link
Owner

Further, services aren't going to have the full user environment even when configured to run as a different user. The most common problem I've found is simply that the PATH isn't what you expect, so the DLLs aren't found (or in strange edge cases, the wrong ones are found). This is exactly why the .exe installer puts the DLLs into system32, and why pip installed pywin32s are always going to have issues in these kinds of contexts.

@yuval9313
Copy link

yuval9313 commented Apr 18, 2020

You were right!
So adding the %Project_DIR%\venv\Lib\site-packages\pywin32_system32 to the global path actually solved the error above, so good job, that's a solution for anyone seek for it in regards to services

Although, now there is another issue which is more relevant to the title:
image

So, what now? how come when pywintypes38.dll is recognized the library is not?
It's important to mention that this error occurs on any import and not just the one in the picture

EDIT:

So I found out that the quick and easy fix is just to install pywin32 outside of venv (on the global interpeter), I'm sorry for anyone who find this solution unsettling but since you wish to change your PC/Server/Whatever, its seems reasonable.

Thank you @CristiFati and @mhammond for the attempts to find a solution.

@mhammond
Copy link
Owner

how come when pywintypes38.dll is recognized the library is not?

There's a pywintypes.py that must be on sys.path

@vernondcole
Copy link
Collaborator

vernondcole commented Apr 23, 2020 via email

@CristiFati
Copy link
Contributor

This is strange. #1442, should take care of this particular case.

Anyway, the original issue reported here doesn't seem to reproduce anymore. Probably it would be OK to create anew one (which only affects services?) I will take a look (give a shot), but only over the weekend.

@mhammond
Copy link
Owner

TBH, I don't think virtualenv is the correct tool to use for services - the stand alone server should be use. Same thing for implementing COM objects which should be made available to all users, such as shell extensions etc.

@CristiFati
Copy link
Contributor

To summarize: a simple import win32api in a plain PyWin32 228 install on a Python 3.8 virtual environment (VirtualEnv or VEnv) should be working now . If anyone experiences some problems, please post the steps to reproduce, and also repeat them on theirs PC before posting here (as the initial setup might be corrupt).

The service problem remains (unfortunately I didn't have the time to confirm / infirm it). Probably this issue should be renamed (and the description changed) to reflect reality, or simply closed and another one open.

In any case, if encountering any problems, please start from a clean initial environment!

@Avasam
Copy link
Collaborator

Avasam commented Mar 13, 2024

To summarize: a simple import win32api in a plain PyWin32 228 install on a Python 3.8 virtual environment (VirtualEnv or VEnv) should be working now . If anyone experiences some problems, please post the steps to reproduce, and also repeat them on theirs PC before posting here (as the initial setup might be corrupt).
[...] or simply closed and another one open.

I agree, OP has been (at least should be) resolved.

The service problem remains (unfortunately I didn't have the time to confirm / infirm it).

Paths and services issues are replicated in other stories as well. Like in #1399 and #1865 (comment) (and many old closed issues with less details)

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

7 participants