错误:在 Python 中查找 IronPDF 失败

This article was translated from English: Does it need improvement?
Translated
View the article in English

假设您已经安装了 .NET 6.0 SDK 和通过 pip 安装了 IronPdf,使用以下命令:

pip install ironpdf

执行 Python 脚本时可能会遇到以下异常:

Exception has occurred: Exception
Failed to locate IronPdf.Slim.dll at 'C:\Users\Name\AppData\Local\Programs\Python\Python311/IronPdf.Slim'. Please see https://ironpdf.com/troubleshooting/quick-ironpdf-troubleshooting/ for more information
  File "C:\Users\Name\OneDrive\Documents\IronPdfPythonNew\working\viewport.py", line 1, in <module>
    from ironpdf import *
Exception: Failed to locate IronPdf.Slim.dll at 'C:\Users\Name\AppData\Local\Programs\Python\Python311/IronPdf.Slim'. Please see https://ironpdf.com/troubleshooting/quick-ironpdf-troubleshooting/ for more information

解决方案

可以按如下方式指定包含 IronPdf.Slim 的目录:

import sys
# This line should point to the root directory that contains the IronPdf.Slim folder.
# Users can determine this location by running `pip uninstall ironpdf` and pressing `N`.
sys.prefix = '/path/to/python/packages'
PYTHON

例如

import sys
# This line should point to the root directory that contains the IronPdf.Slim folder.
# Users can determine this location by running `pip uninstall ironpdf` and pressing `N`.
sys.prefix = r'C:\Users\lyty1\AppData\Local\Programs\Python\Python311'

from ironpdf import *

# Instantiate Renderer
renderer = ChromePdfRenderer()

# Create a PDF from a URL or local file path
pdf = renderer.RenderUrlAsPdf("https://ironpdf.com/")

# Export to a file or Stream
pdf.SaveAs("url.pdf")
PYTHON

原因

大多数第三方库安装在名为“site-packages”的目录中,该目录位于Python安装目录内,或者如果您使用虚拟环境的话,则位于虚拟环境目录内。 这是您的Python解释器可以访问的目录。

IronPDF 将安装 IronPdf.SlimIronPdf.Native.Chrome.Windows

根据操作系统和您的Python配置,确切位置可能会有所不同。