pip --version 导致打开引发 FileNotFoundError("无法打开孤立路径")

edd*_*die 1 python api python-2.7 python-3.x python-requests

我正在努力从 Python 调用 Rest-API。我已经使用 Postman 测试了所有 Rest-API,并且工作正常。但是,在使用 Python 脚本执行这些脚本时,我遇到了认证错误。

我正在从 Windows cmd 执行 python 脚本。

下面是代码:

import requests
import certifi
from urllib.request import urlopen
import ssl

requestCert = 'https://someurl.com:4443/api/11/projects/'
urlopen(requestCert, context=ssl.create_default_context(cafile=certifi.where()))

headers = {
    "Authorization": "fdsfsfdewrwerwer",
    "X-Auth-Token": "YxzcfhnkniGVGljghjmwCIGLfhfsfdzxc4o5sSADtaT2"
}

#response = requests.get('https://someurl.com:4443/api/11/projects', headers=headers)
#response = requests.get('https://someurl.com:4443/api/11/projects', headers=headers, verify=certifi.where())
response = requests.get('https://someurl.com:4443/api/11/projects', headers=headers, verify='server.pem')

print(response)
Run Code Online (Sandbox Code Playgroud)

进一步调试。。。即使简单import requests print('Hello, world!')pip install requests给出相同的错误。我想问题不在代码中,而是与模块导入功能有关。

我收到的错误如下。

Traceback (most recent call last):
  File "C:\Users\eddie\Desktop\PyPoc\Py_Job\importJob.py", line 1, in <module>
    import requests
  File "C:\Users\eddie\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\__init__.py", line 147, in <module>
    from . import packages, utils
  File "C:\Users\eddie\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\utils.py", line 24, in <module>
    from . import certs
  File "C:\Users\eddie\AppData\Local\Programs\Python\Python311\Lib\site-packages\requests\certs.py", line 14, in <module>
    from certifi import where
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "C:\Users\eddie\AppData\Local\Programs\Python\Python311\Lib\site-packages\wrapt\importer.py", line 177, in _exec_module
    notify_module_loaded(module)
  File "C:\Users\eddie\AppData\Local\Programs\Python\Python311\Lib\site-packages\wrapt\wrappers.py", line 578, in __call__
    return self._self_wrapper(self.__wrapped__, self._self_instance,
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\eddie\AppData\Local\Programs\Python\Python311\Lib\site-packages\wrapt\decorators.py", line 470, in _synchronized
    return wrapped(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\eddie\AppData\Local\Programs\Python\Python311\Lib\site-packages\wrapt\importer.py", line 136, in notify_module_loaded
    hook(module)
  File "C:\Users\eddie\AppData\Local\Programs\Python\Python311\Lib\site-packages\certifi_win32\wrapt_certifi.py", line 20, in apply_patches
    certifi_win32.wincerts.CERTIFI_PEM = certifi.where()
                                         ^^^^^^^^^^^^^^^
  File "C:\Users\eddie\AppData\Local\Programs\Python\Python311\Lib\site-packages\certifi\core.py", line 37, in where
    _CACERT_PATH = str(_CACERT_CTX.__enter__())
                       ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\eddie\AppData\Local\Programs\Python\Python311\Lib\contextlib.py", line 137, in __enter__
    return next(self.gen)
           ^^^^^^^^^^^^^^
  File "C:\Users\eddie\AppData\Local\Programs\Python\Python311\Lib\importlib\resources\_common.py", line 80, in _tempfile
    os.write(fd, reader())
                 ^^^^^^^^
  File "C:\Users\eddie\AppData\Local\Programs\Python\Python311\Lib\importlib\resources\abc.py", line 76, in read_bytes
    with self.open('rb') as strm:
         ^^^^^^^^^^^^^^^
  File "C:\Users\eddie\AppData\Local\Programs\Python\Python311\Lib\importlib\resources\_adapters.py", line 141, in open
    raise FileNotFoundError("Can't open orphan path")
FileNotFoundError: Can't open orphan path
Run Code Online (Sandbox Code Playgroud)

你能告诉我我需要做什么改变吗?

Chr*_* H. 6

这个 StackOverFlow 问题也有同样的问题,并且提供的解决方案对我有用。

从Python 库文件夹中删除以下两个文件(例如,如果您使用的是 Windows + Python 3.10 并且没有 venv/conda,则为 C:\Users<用户名>\AppData\Local\Programs\Python\Python310\Lib\site-packages) :

 python-certifi-win32-init.pth
 distutils-precedence.pth
Run Code Online (Sandbox Code Playgroud)

之后你可以使用

python -m pip uninstall python-certifi-win32
Run Code Online (Sandbox Code Playgroud)

卸载该软件包并安装 pip-system-certs,这应该允许您使用 SSL 请求。

  • 谢谢。安装 python-certifi-win32 正是我陷入困境的原因! (2认同)