錯誤:未能找到 IronPDF in Python

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

假設您已經安裝了 .NET 6.0 SDK 和 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配置,確切位置可能會有所不同。