在导入 xlwings 或使用 pip 时遇到 ValueError()

Encountering ValueError() on importing xlwings or using pip

我在 PyCharm 项目中尝试导入 xlwings 时遇到以下错误:

C:\Users\[USERNAME]\venv\Scripts\python.exe "C:\Program Files\JetBrains\PyCharm 2021.3.2\plugins\python\helpers\pydev\pydevconsole.py" --mode=client --port=62345
import sys; print('Python %s on %s' % (sys.version, sys.platform))
sys.path.extend(['C:\Users\[USERNAME]\PycharmProjects\Charge_Corrections_Setup', 'C:/Users/[USERNAME]/PycharmProjects/Charge_Corrections_Setup'])
PyDev console: starting.
Python 3.10.2 (tags/v3.10.2:a58ebcc, Jan 17 2022, 14:12:15) [MSC v.1929 64 bit (AMD64)] on win32
>>> import xlwings as xw
Traceback (most recent call last):
  File "C:\Users\[USERNAME]\AppData\Local\Programs\Python\Python310\lib\importlib\_common.py", line 89, in _tempfile
    os.write(fd, reader())
  File "C:\Users\[USERNAME]\AppData\Local\Programs\Python\Python310\lib\importlib\abc.py", line 371, in read_bytes
    with self.open('rb') as strm:
  File "C:\Users\[USERNAME]\AppData\Local\Programs\Python\Python310\lib\importlib\_adapters.py", line 54, in open
    raise ValueError()
ValueError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\[USERNAME]\AppData\Local\Programs\Python\Python310\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "C:\Program Files\JetBrains\PyCharm 2021.3.2\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\[USERNAME]\venv\lib\site-packages\xlwings\__init__.py", line 36, in <module>
    from .main import (
  File "C:\Program Files\JetBrains\PyCharm 2021.3.2\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\[USERNAME]\venv\lib\site-packages\xlwings\main.py", line 2754, in <module>
    from . import conversion
  File "C:\Program Files\JetBrains\PyCharm 2021.3.2\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\[USERNAME]\venv\lib\site-packages\xlwings\conversion\__init__.py", line 19, in <module>
    from .standard import (
  File "C:\Program Files\JetBrains\PyCharm 2021.3.2\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\[USERNAME]\venv\lib\site-packages\xlwings\conversion\standard.py", line 12, in <module>
    from ..pro import Markdown
  File "C:\Program Files\JetBrains\PyCharm 2021.3.2\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\[USERNAME]\venv\lib\site-packages\xlwings\pro\__init__.py", line 15, in <module>
    from .embedded_code import runpython_embedded_code, dump_embedded_code
  File "C:\Program Files\JetBrains\PyCharm 2021.3.2\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\[USERNAME]\venv\lib\site-packages\xlwings\pro\embedded_code.py", line 22, in <module>
    from .module_permissions import verify_execute_permission
  File "C:\Program Files\JetBrains\PyCharm 2021.3.2\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\[USERNAME]\venv\lib\site-packages\xlwings\pro\module_permissions.py", line 22, in <module>
    import requests
  File "C:\Program Files\JetBrains\PyCharm 2021.3.2\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\[USERNAME]\venv\lib\site-packages\requests\__init__.py", line 133, in <module>
    from . import utils
  File "C:\Program Files\JetBrains\PyCharm 2021.3.2\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\[USERNAME]\venv\lib\site-packages\requests\utils.py", line 27, in <module>
    from . import certs
  File "C:\Program Files\JetBrains\PyCharm 2021.3.2\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\[USERNAME]\venv\lib\site-packages\requests\certs.py", line 15, in <module>
    from certifi import where
  File "C:\Program Files\JetBrains\PyCharm 2021.3.2\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "C:\Users\[USERNAME]\venv\lib\site-packages\wrapt\importer.py", line 170, in exec_module
    notify_module_loaded(module)
  File "C:\Users\[USERNAME]\venv\lib\site-packages\wrapt\decorators.py", line 470, in _synchronized
    return wrapped(*args, **kwargs)
  File "C:\Users\[USERNAME]\venv\lib\site-packages\wrapt\importer.py", line 136, in notify_module_loaded
    hook(module)
  File "C:\Users\[USERNAME]\venv\lib\site-packages\certifi_win32\wrapt_certifi.py", line 20, in apply_patches
    certifi_win32.wincerts.CERTIFI_PEM = certifi.where()
  File "C:\Users\[USERNAME]\venv\lib\site-packages\certifi\core.py", line 37, in where
    _CACERT_PATH = str(_CACERT_CTX.__enter__())
  File "C:\Users\[USERNAME]\AppData\Local\Programs\Python\Python310\lib\contextlib.py", line 135, in __enter__
    return next(self.gen)
  File "C:\Users\[USERNAME]\AppData\Local\Programs\Python\Python310\lib\importlib\_common.py", line 95, in _tempfile
    os.remove(raw_path)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\Users\[USERNAME]\AppData\Local\Temp\tmp_g611hdf'
import xlwings as xw
Traceback (most recent call last):
  File "C:\Users\[USERNAME]\AppData\Local\Programs\Python\Python310\lib\importlib\_common.py", line 89, in _tempfile
    os.write(fd, reader())
  File "C:\Users\[USERNAME]\AppData\Local\Programs\Python\Python310\lib\importlib\abc.py", line 371, in read_bytes
    with self.open('rb') as strm:
  File "C:\Users\[USERNAME]\AppData\Local\Programs\Python\Python310\lib\importlib\_adapters.py", line 54, in open
    raise ValueError()
ValueError
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "C:\Users\[USERNAME]\AppData\Local\Programs\Python\Python310\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "C:\Program Files\JetBrains\PyCharm 2021.3.2\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\[USERNAME]\venv\lib\site-packages\xlwings\__init__.py", line 36, in <module>
    from .main import (
  File "C:\Program Files\JetBrains\PyCharm 2021.3.2\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\[USERNAME]\venv\lib\site-packages\xlwings\main.py", line 2754, in <module>
    from . import conversion
  File "C:\Program Files\JetBrains\PyCharm 2021.3.2\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\[USERNAME]\venv\lib\site-packages\xlwings\conversion\__init__.py", line 19, in <module>
    from .standard import (
  File "C:\Program Files\JetBrains\PyCharm 2021.3.2\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\[USERNAME]\venv\lib\site-packages\xlwings\conversion\standard.py", line 12, in <module>
    from ..pro import Markdown
  File "C:\Program Files\JetBrains\PyCharm 2021.3.2\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\[USERNAME]\venv\lib\site-packages\xlwings\pro\__init__.py", line 15, in <module>
    from .embedded_code import runpython_embedded_code, dump_embedded_code
  File "C:\Program Files\JetBrains\PyCharm 2021.3.2\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\[USERNAME]\venv\lib\site-packages\xlwings\pro\embedded_code.py", line 22, in <module>
    from .module_permissions import verify_execute_permission
  File "C:\Program Files\JetBrains\PyCharm 2021.3.2\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\[USERNAME]\venv\lib\site-packages\xlwings\pro\module_permissions.py", line 22, in <module>
    import requests
  File "C:\Program Files\JetBrains\PyCharm 2021.3.2\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\[USERNAME]\venv\lib\site-packages\requests\__init__.py", line 133, in <module>
    from . import utils
  File "C:\Program Files\JetBrains\PyCharm 2021.3.2\plugins\python\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "C:\Users\[USERNAME]\venv\lib\site-packages\requests\utils.py", line 42, in <module>
    DEFAULT_CA_BUNDLE_PATH = certs.where()
  File "C:\Users\[USERNAME]\venv\lib\site-packages\certifi\core.py", line 37, in where
    _CACERT_PATH = str(_CACERT_CTX.__enter__())
  File "C:\Users\[USERNAME]\AppData\Local\Programs\Python\Python310\lib\contextlib.py", line 135, in __enter__
    return next(self.gen)
  File "C:\Users\[USERNAME]\AppData\Local\Programs\Python\Python310\lib\importlib\_common.py", line 95, in _tempfile
    os.remove(raw_path)
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: 'C:\Users\[USERNAME]\AppData\Local\Temp\tmpkn_ssllb'

第二部分似乎是访问日志的问题,所以我认为没关系,但 ValueError 令人担忧。这是一系列导入的一部分,包括 xlrd、openpyxl、xlsxwriter 等,所以奇怪的是只有这个模块抛出错误。

然而,一旦我开始进行故障排除,我发现了更奇怪的事情:任何 pip 的使用都会引发相同系列的错误:

PS C:\Users\[USERNAME]\PycharmProjects\Reports> pip install seaborn
Traceback (most recent call last):
  File "C:\Users\[USERNAME]\AppData\Local\Programs\Python\Python310\lib\importlib\_common.py", line 89, in _tempfile
    os.write(fd, reader())
  File "C:\Users\[USERNAME]\AppData\Local\Programs\Python\Python310\lib\importlib\abc.py", line 371, in read_bytes
    with self.open('rb') as strm:
  File "C:\Users\[USERNAME]\AppData\Local\Programs\Python\Python310\lib\importlib\_adapters.py", line 54, in open
    raise ValueError()
ValueError

它还会抛出文件访问问题,但我忽略了它 space。

我不确定这里的问题是什么,甚至不知道应该去哪里查看。我们刚刚在工作中更新了我们的 Web 代理,但我昨天遇到了这个问题,并且相信我已经通过在环境和 PyCharm 设置中更新证书来解决它。这样做之后 pip 和所有程序在昨晚注销之前都在工作,但是今天早上这里的第一件事就是这个问题。

代理可能是问题所在,但我什至不知道从哪里开始寻找诊断我尚未完成的诊断,尤其是因为值错误不具体。

如果有人知道在哪里查看或根本原因可能是什么,我将非常感激!

为了防止将来有人遇到此问题,已通过完全清除我的 PyCharm 安装并从头开始解决此问题。

尽我所能,这是由于安装 python-certifi-win32 软件包并使用相当复杂的 Web 代理造成的。 PyCharm 已经对代理提供的证书很挑剔,让 python-certifi-win32 干预它们似乎已经走了很远。

过去似乎已经记录了类似问题的案例(请参阅 HERE),但代理的添加将一个烦人的问题变成了一个损坏的问题,完全破坏了 PIP,甚至不允许卸载包裹。

由于这对我的组织的设置来说非常具体,我怀疑其他人会遇到这种情况,但为了以防万一,也许这会有所帮助。