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 install之后,它不起作用。

示例:

[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 em>)
  • 可在 64 位32 位 上重现

最佳答案

剧透警告!!!

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

安装(其中一个)后,现有代码应该可以工作OOTB(就这个问题而言)。

安装步骤:

  1. 下载与您的 Python 架构(64 位32 位 - 了解更多信息)相匹配的 .whl有关获取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模块,在这种情况下它不属于上述规则)<强>在任何 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/58661973/

相关文章:

python - 在正则表达式中,如何匹配字符串的两种不同情况

c - 我想要更大的 pixbuf 渲染到 GtkSource Gutter

c# - 无法加载DLL : The specified module could not be found

Windows API 解释器

python - 在 GDB python 接口(interface)下导入 wx 失败,R6034 : An application has made an attempt to load the C runtime library incorrectly

c - Visual Studio : Unresolved external symbols after adding new DLL APIs

python更改某些两个单词之间的字符串

python - pygtk中关于对话框不关闭对话框的关闭按钮

python - 如何避免 PyGame 中圆形和矩形之间的小故障碰撞?

c# - 为什么我的 ID 会随着互联网连接而改变?