首页 > 解决方案 > Python 无法识别我的 PATH 配置

问题描述

我正在尝试在 python 中使用一些库,但我遇到了 PATH 配置问题。

当我尝试使用 Selenium 和 pdf2image 时会发生这种情况。在这两种情况下,都会有一条消息询问文件夹是否在 PATH 中。我已经检查过了,它们已作为系统变量添加到 PATH 中,如您在屏幕截图中所见。

附加信息:当我打开 Ubuntu 时,会出现附加消息,通知 pyenv 配置错误。这会是我的问题的原因吗?我尝试按照说明更改文件 .zhrc 和 .profile 来修复它(如您在屏幕截图中所见),但它仍然无法正常工作。

pdf2image 示例:

输入

# Import libraries

from PIL import Image
import pytesseract
import sys
from pdf2image import convert_from_path, convert_from_bytes
import os

PDF_file = "caderno2_2020-10-28.pdf"

pages = convert_from_path(PDF_file, 500, poppler_path='C:\Program Files\poppler-0.68.0\bin')

输出

FileNotFoundError                         Traceback (most recent call last)
~/.pyenv/versions/3.8.6/envs/ebadados_balancos/lib/python3.8/site-packages/pdf2image/pdf2image.py in pdfinfo_from_path(pdf_path, userpw, poppler_path, rawdates, timeout)
    457             env["LD_LIBRARY_PATH"] = poppler_path + ":" + env.get("LD_LIBRARY_PATH", "")
--> 458         proc = Popen(command, env=env, stdout=PIPE, stderr=PIPE)
    459 

~/.pyenv/versions/3.8.6/lib/python3.8/subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors, text)
    853 
--> 854             self._execute_child(args, executable, preexec_fn, close_fds,
    855                                 pass_fds, cwd, env,

~/.pyenv/versions/3.8.6/lib/python3.8/subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, restore_signals, start_new_session)
   1701                         err_msg = os.strerror(errno_num)
-> 1702                     raise child_exception_type(errno_num, err_msg, err_filename)
   1703                 raise child_exception_type(err_msg)

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Program Files\\poppler-0.68.0\x08in/pdfinfo'

During handling of the above exception, another exception occurred:

PDFInfoNotInstalledError                  Traceback (most recent call last)
/tmp/ipykernel_4418/1334408248.py in <module>
     14 
     15 # Store all the pages of the PDF in a variable
---> 16 pages = convert_from_path(PDF_file, 500, poppler_path='C:\Program Files\poppler-0.68.0\bin')
     17 
     18 

~/.pyenv/versions/3.8.6/envs/ebadados_balancos/lib/python3.8/site-packages/pdf2image/pdf2image.py in convert_from_path(pdf_path, dpi, output_folder, first_page, last_page, fmt, jpegopt, thread_count, userpw, use_cropbox, strict, transparent, single_file, output_file, poppler_path, grayscale, size, paths_only, use_pdftocairo, timeout, hide_annotations)
     96         poppler_path = poppler_path.as_posix()
     97 
---> 98     page_count = pdfinfo_from_path(pdf_path, userpw, poppler_path=poppler_path)["Pages"]
     99 
    100     # We start by getting the output format, the buffer processing function and if we need pdftocairo

~/.pyenv/versions/3.8.6/envs/ebadados_balancos/lib/python3.8/site-packages/pdf2image/pdf2image.py in pdfinfo_from_path(pdf_path, userpw, poppler_path, rawdates, timeout)
    482 
    483     except OSError:
--> 484         raise PDFInfoNotInstalledError(
    485             "Unable to get page count. Is poppler installed and in PATH?"
    486         )

PDFInfoNotInstalledError: Unable to get page count. Is poppler installed and in PATH?

硒的例子

输入

import selenium

#Simple assignment
from selenium.webdriver import Chrome

driver = Chrome(executable_path='C:\\Program Files\\Google\\Chrome\\chromedriver_win32')

输出

FileNotFoundError                         Traceback (most recent call last)
~/.pyenv/versions/3.8.6/envs/ebadados_balancos/lib/python3.8/site-packages/selenium/webdriver/common/service.py in start(self)
     73             cmd.extend(self.command_line_args())
---> 74             self.process = subprocess.Popen(cmd, env=self.env,
     75                                             close_fds=system() != 'Windows',

~/.pyenv/versions/3.8.6/lib/python3.8/subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds, encoding, errors, text)
    853 
--> 854             self._execute_child(args, executable, preexec_fn, close_fds,
    855                                 pass_fds, cwd, env,

~/.pyenv/versions/3.8.6/lib/python3.8/subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, restore_signals, start_new_session)
   1701                         err_msg = os.strerror(errno_num)
-> 1702                     raise child_exception_type(errno_num, err_msg, err_filename)
   1703                 raise child_exception_type(err_msg)

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Program Files\\Google\\Chrome\\chromedriver_win32'

During handling of the above exception, another exception occurred:

WebDriverException                        Traceback (most recent call last)
/tmp/ipykernel_4554/1789374845.py in <module>
      2 from selenium.webdriver import Chrome
      3 
----> 4 driver = Chrome(executable_path='C:\\Program Files\\Google\\Chrome\\chromedriver_win32')
      5 

~/.pyenv/versions/3.8.6/envs/ebadados_balancos/lib/python3.8/site-packages/selenium/webdriver/chrome/webdriver.py in __init__(self, executable_path, port, options, service_args, desired_capabilities, service_log_path, chrome_options, service, keep_alive)
     67             service = Service(executable_path, port, service_args, service_log_path)
     68 
---> 69         super(WebDriver, self).__init__(DesiredCapabilities.CHROME['browserName'], "goog",
     70                                         port, options,
     71                                         service_args, desired_capabilities,

~/.pyenv/versions/3.8.6/envs/ebadados_balancos/lib/python3.8/site-packages/selenium/webdriver/chromium/webdriver.py in __init__(self, browser_name, vendor_prefix, port, options, service_args, desired_capabilities, service_log_path, service, keep_alive)
     88 
     89         self.service = service
---> 90         self.service.start()
     91 
     92         try:

~/.pyenv/versions/3.8.6/envs/ebadados_balancos/lib/python3.8/site-packages/selenium/webdriver/common/service.py in start(self)
     82         except OSError as err:
     83             if err.errno == errno.ENOENT:
---> 84                 raise WebDriverException(
     85                     "'%s' executable needs to be in PATH. %s" % (
     86                         os.path.basename(self.path), self.start_error_message)

WebDriverException: Message: 'C:\Program Files\Google\Chrome\chromedriver_win32' executable needs to be in PATH. Please see https://chromedriver.chromium.org/home

pyenv 根据 ubuntu 消息配置错误

路径配置

.zhrc 文件按照 ubuntu 消息中的建议配置

.profile 文件按照 ubuntu 消息中的建议配置

标签: pathselenium-chromedriverpyenvpdf2image

解决方案


推荐阅读