python - PyWin32 和 Python 3.8.0

标签 python windows dll pywin32 python-3.8

Python 3.8.0 最近发布了(在 20191014 上,可以从 [Python]: Python 3.8.0 下载)。
PyWin32[PyPI]: pywin32 225 上构建了它(于 20190915 发布)。不幸的是,在 pip installing 之后,它不起作用。

示例:

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q058631512]> sopr.bat
*** Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ***

[prompt]> "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\Scripts\python.exe"
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import win32api
>>> ^Z


[prompt]> "e:\Work\Dev\VEnvs\py_064_03.08.00_test0\Scripts\python.exe"
Python 3.8.0 (tags/v3.8.0:fa919fd, Oct 14 2019, 19:37:50) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import win32api
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: DLL load failed while importing win32api: The specified module could not be found.
>>> ^Z

注意事项:

  • 对于 Python 3.7,我也将我的 PyWin32 模块升级到最新版本,并且可以正常工作
  • 较旧的 PyWin32 版本适用于较旧的 Python 版本(2.73.53.6)
  • 可在64 位32 位 上重现

最佳答案

剧透警告!!!

#2.2.(从下方)应用于原始.whl,并在 [GitHub]: CristiFati/Prebuilt-Binaries - (master) Prebuilt-Binaries/PyWin32/v225 上发布它们(win_amd64win32 用于 Python 3.8)。

安装(其中一个)后,现有代码应该可以OOTB(关于这个问题)。

安装步骤:

  1. 下载与您的 Python 架构相匹配的 .whl(64 位32 位 - 更多有关获取 Python 架构的详细信息,请查看 [SO]: How do I determine if my python shell is executing in 32bit or 64bit? (@CristiFati's answer)(问题是关于 OSX,但也涵盖其他平台)),它很可能是 64 位 (win_amd64),来自上面的 URL
    比如我下载到L:\Downloads

  2. 在其上调用 PIP 安装程序 ( [SO]: How to install a package for a specific Python version on Windows 10? (@CristiFati's answer) )。像这样的东西:

    (${path_to_your})python.exe -m pip ${path_to_the_downloaded_pywin32_whl}
    

    例子:

    "e:\Work\Dev\VEnvs\py_pc064_03.08.00_test0\Scripts\python.exe" -m pip "L:\Downloads\pywin32-225-cp38-cp38-win_amd64.whl"
    


问题已在 [GitHub]: mhammond/pywin32 - python 3.8 上报告.

上面的 URL 还引用了 2 个:

  • [Python 3.8.Docs]: What’s New In Python 3.8 - Changes in the Python API其中指出(强调是我的):

    • DLL dependencies for extension modules and DLLs loaded with ctypes on Windows are now resolved more securely. Only the system paths, the directory containing the DLL or PYD file, and directories added with add_dll_directory() are searched for load-time dependencies. Specifically, PATH and the current working directory are no longer used, and modifications to these will no longer have any effect on normal DLL resolution.
  • [Python 3.Docs]: os.add_dll_directory(path)其中指出(强调 仍然是我的):

    This search path is used when resolving dependencies for imported extension modules (the module itself is resolved through sys.path), and also by ctypes.

与此同时,我自己进行了一些挖掘并发现(对于 win32api.pyd)它是 pywintypes38.dll(它是 < em>.pyds) 未找到(我还在对该问题的评论中指定了这一点)。

解决方案(实际上是解决方法(或多或少),直到发布官方和向后兼容的修复程序):

  1. 通过导入强制加载 pywintypes38.dll(因为它也是一个 Python 模块,在这种情况下它不属于上述规则)< strong>在任何 PyWin32 模块之前:

    import pywintypes
    import win32api
    

    如果使用 COM,您需要 import pythoncom

  2. pywin32_system32 添加到 .dll 搜索路径(遵循上面的新模型)。有多种方式:

    1. v-python 对问题 URL 的评论提供了一个小片段(我没有测试)

    2. 我也提交了 [GitHub]: mhammond/pywin32 - Support for Python 3.8 ,我在 pywin32.pth 文件中执行所有操作(解释器启动时“执行”,因此现有代码无需更改)。不幸的是,AppVeyor 自动化测试有一个问题,它失败了(但由于其他一些原因),所以它已经在那里停留了一段时间。请注意,与此同时,PR 已关闭,并推出了另一种(类似的)方法。请注意,包含此修复程序的 v226(发布于 20191110)不适用于 VirtualEnv ([SO]: PyWin32 (226) and virtual environments (@CristiFati's answer))。
      无论如何,在本地应用更改(1)(在我的 Python VirtualEnv 上s),解决了问题(在一个上,并且没有破坏另一个):

      [cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q058631512]> sopr.bat
      ### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###
      
      [prompt]> "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\Scripts\python.exe" -c "import win32api"
      
      [prompt]> "e:\Work\Dev\VEnvs\py_064_03.08.00_test0\Scripts\python.exe" -c "import win32api"
      
      [prompt]>
      
    3. 其他方式,例如复制 .dll(例如在 %SystemRoot%\System32 中),或对它们进行符号链接(symbolic link),但(就个人而言)我不会推荐那些

有关.dll 加载(通过CTypes)的更多详细信息,请查看[SO]: Can't import dll module in Python (@CristiFati's answer) .



更新#0

[PyPI]: pywin32 227 (解决了这个问题),发表于 20191114!



脚注

关于python - PyWin32 和 Python 3.8.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58631512/

相关文章:

windows - 如何获得上下文菜单选择的路径?

windows - 如何使用 CMake 生成 Windows DLL 版本控制信息?

delphi - 如何使用系统路径之外的dll

c++ - 使用 CMake 链接库的调试版本

python - 如何使用 python 过滤时间序列或数据框中的日期范围

python - PyTorch 自定义转换,在 __call__ 中带有附加参数

python - 高效查找两个列表之间的元素差异

linux - 删除 Maven Pom 中的重复依赖项

C# 编码(C# 调用 C++ DLL)

python - 如何将 popen 与命令行参数一起使用包含单引号和双引号?