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

pip install pywin32 inside Conda Python 3.8/3.9 env causes DLL module not found error #1865

Closed
eddiebergman opened this issue Apr 22, 2022 · 7 comments

Comments

@eddiebergman
Copy link

eddiebergman commented Apr 22, 2022

Hello, thanks for the awesome library :)

# Sys info
Python 3.7 | 3.8
Conda
pywin32==303

I apologies if this is a dupe but I didn't find a clean answer given the various installation methods.

When using conda with Python 3.8 or 3.9 and install pywin32 with pip install pywin32, the following error is raised:

# Inside a conda env
# ./mypackage, a source folder which requires `pywin32`
$ cat mypackage/src/__init__.py
> import win32api

$ pip install mypackage
$ python -c "import mypackage"
> ImportError: DLL load failed while importing win32api: The specified module could not be found.

Simple Solution for others reading

The easiest fix for most cases is to simply:

  • pip uninstall pywin32; conda install pywin32.

Failing that, please see the README.md about the post_install script:

This was not required in every other case


However we are developing a cross-platform package pynisher that uses pywin32 for limiting memory of processes for compatibility on windows platforms. This package is mostly a dependency for research tools which are mostly just source code hosted on github and installed using git clone <x>; pip install <x>, we also don't have experience with releasing to conda. I would imagine many of these users will also be using conda environments. Unfortunately, as far as I'm aware conda install <x> will not work on local source code and so recommending git clone <x>; conda install <x> will not work and requires <x> to be built first. As a further step, part of our CI is testing installation of packages in conda envs with pip install. All dependant tools would require updating the CI to perform the above fix.

While the above solution works, it would require all dependent tools depending on pynisher to include explicit steps on how to fix this issue. Ideally, this could be resolved within pynisher's installation but I'm not sure how and what the best solution is.

  1. Have all dependant tools be forced to include this extra fix step.
  2. Have a post_install inside setup.py for pynisher which calls the post_install script.
    • If this is the case, would there be side effects left over after something like pip uninstall pywin32 is run or the virtual env destroyed?
    • As a side note this comment seems a bit too strong as doing dist installs and normal python virtualenv setups work fine with pip install pywin32. I've tested the following setups in our github actions CI and the issue is only with the described setup of conda and 3.8 or 3.9.
      > python pywin32_postinstall.py -install
      If you installed pywin32 via a .exe installer, this should be run
      automatically after installation, but if it fails you can run it again.
      If you installed pywin32 via PIP, you almost certainly need to run this to
      setup the environment correctly.

Is this something that is even possible to fix within pywin32 or is the scope of the problem related to conda and python 3.8/3.9?

Apologies for the long post, I understand it's an error of interaction and I hope this could hopefully serve as something informative to any other people facing this particular packaging issue!

@vernondcole
Copy link
Collaborator

vernondcole commented Apr 23, 2022 via email

@eddiebergman
Copy link
Author

eddiebergman commented Apr 23, 2022

Hi @vernondcole,

Thanks for the pointers one possible solution I tried was:

setup(
    install_requires=[
        "pywin ; platform==Windows ; python_version==3.7 or python_version >= 3.10",
        "pywin32==228 ; platform==Windows ; python_version==3.8 or python_version==3.9",
    ]
)

Unfortunately, it doesn't seem like there's a straightforward way to specify requirements based on the env it's running in, i.e. virtaulenv or conda, so this will needlessly effect non conda users.

I also see some issues with this with respect to dependencies, i.e. some package requires pywin32 >= 300. Our university makes machine learning tools which are often used in upstream applications or Jupyter environments, giving a dependency chain such as app_package -> machine_learning_package -> optimizer_package -> pynisher. Locking a version could provide quite a few upstream issues.

The pip documentation for pip install says it shouldn't try to update pywin32 if it can find it already, so I assume anaconda is somehow hiding this from pip which causes it to install the newest compatible version, instead of using the one that is there.

It doesn't seem like there's anything you can do at the pywin32 level and I don't think it's something conda will fix anytime soon. My solution is to disable the feature relying on pywin32 if it fails to import and just include documentation which effected users will have to read and do, i.e. pip uninstall pywin32; conda install pywin32.

Please feel free to close this issue, I hope it can help anyone else encountering this issue and it will be useful to link in documentation!

Please feel free to close :)

@skewty
Copy link

skewty commented Apr 27, 2022

In order to get services working I had to make these changes:

cd /d C:\Program Files\Python310\lib\site-packages

# fix for python windows service .DLL errors
copy pywin32_system32\*.dll ..\..
copy win32\pythonservice.exe ..\..

# fix for .DLL naming issue with logging.handlers.NTEventLogHandler
copy win32\win32service.*.pyd win32\win32service.pyd

This doesn't quite seem to be enough though. My Windows Event Error log is still showing entries for my service using Event ID numbers I don't understand / recognize (aren't in my code or in pywin32 code (example servicemanager.py). Any ideas about other fixes / tweaks required?

@yezhengli-Mr9
Copy link

yezhengli-Mr9 commented Oct 27, 2023

Is pip no longer supported? Or these relevant repo only support on Windows operation system?

python -m pip install --upgrade pywin32

@vernondcole
Copy link
Collaborator

vernondcole commented Oct 28, 2023 via email

@Avasam
Copy link
Collaborator

Avasam commented Nov 1, 2023

setup(
    install_requires=[
        "pywin ; platform==Windows ; python_version==3.7 or python_version >= 3.10",
        "pywin32==228 ; platform==Windows ; python_version==3.8 or python_version==3.9",
    ]
)

[...]
The pip documentation for pip install says it shouldn't try to update pywin32 if it can find it already, so I assume anaconda is somehow hiding this from pip which causes it to install the newest compatible version, instead of using the one that is there.

AFAIK Conda doesn't support PEP 508 Environment markers. (see conda/conda#11198)
It does however have something called Preprocessing selectors, but I never looked into it much. (conda/conda#676 (comment))

(maybe also related: conda/conda#8089 and conda/conda#7258)

@Avasam
Copy link
Collaborator

Avasam commented Mar 13, 2024

Sending the ball in Conda's camp for this one, given that the issue seems to be caused by an older version of pywin32 being shipped with Conda, with the solution always being "reinstall or upgrade pywin32":

We can re-open if there's something pywin32 can do with a concrete plan to alleviate issues with Conda (and isn't duplicating another issue)

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

5 participants