简体   繁体   English

pip --version led to open raise FileNotFoundError("Can't open orphan path")

[英]pip --version led to open raise FileNotFoundError("Can't open orphan path")

I am working on calling the Rest-API from Python. All the Rest-API I have tested using Postman and are working fine.我正在努力从 Python 调用 Rest-API。我使用 Postman 测试过的所有 Rest-API 都运行良好。 But, While executing those using Python scripts I am facing certification error.但是,在执行那些使用 Python 脚本的脚本时,我遇到了认证错误。

I am executing the python script from windows cmd.我正在执行来自 windows cmd 的 python 脚本。

Below is the code:下面是代码:

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)

Further debugging.进一步调试。 . . . . even simple import requests print('Hello, world!') and pip install requests giving same error.即使是简单import requests print('Hello, world!')pip install requests给出相同的错误。 I guess the issue is not in the code, it's something with the module import functionality.我想问题不在代码中,它与模块导入功能有关。

The error that I am getting is as below.我得到的错误如下。

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

Can you please let me what changes I need to do?你能告诉我我需要做什么改变吗?

Maybe you can try to check the path where Python interpreter import packages.也许您可以尝试检查 Python 解释器导入包的路径。

import sys
print(sys.path)

Run the snippet both in IDE and command line to figure out where does the Python interpreter find packages and whether it is different from where you install packages with pip.在 IDE 和命令行中运行代码片段,以确定 Python 解释器在哪里找到包,以及它是否与您使用 pip 安装包的位置不同。

This StackOverFlow question has the same issue and the solution provided worked for me.这个 StackOverFlow 问题有同样的问题,提供的解决方案对我有用。

Remove the following two files from your Python library folder (eg, C:\Users<username>\AppData\Local\Programs\Python\Python310\Lib\site-packages if you are using Windows + Python 3.10 and no venv/conda):Python 库文件夹中删除以下两个文件(例如,C:\Users<用户名>\AppData\Local\Programs\Python\Python310\Lib\site-packages 如果您使用的是 Windows + Python 3.10 并且没有 venv/conda) :

 python-certifi-win32-init.pth
 distutils-precedence.pth

After this you can use在此之后你可以使用

python -m pip uninstall python-certifi-win32

to uninstall the package and install pip-system-certs instead, which should allow you to work with SSL requests.卸载 package 并改为安装 pip-system-certs,这应该允许您处理 SSL 请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在Python 3.5版中打开点子 - Can't open pip in Python version 3.5 Python 无法打开文本文件:“FileNotFoundError” - Python can't open text file: “FileNotFoundError” 使用 mock_open 引发 FileNotFoundError 并检查是否调用了 write - Using mock_open to raise a FileNotFoundError and check if write is called Pip install:无法打开文件pip,或者父模块''未加载 - Pip install: can't open file pip, or Parent module '' not loaded Python路径:为什么看不到numpy的最新版本 - Python path: why can't pip see latest version of numpy 子进程 FileNotFoundError: [Errno 2] 在 PATH 中打开外部程序时没有这样的文件或目录 - subprocess FileNotFoundError: [Errno 2] No such file or directory when open external program in PATH 无法打开文件 &#39;pip&#39;: [Errno 2] 没有那个文件或目录 - can't open file 'pip': [Errno 2] No such file or directory pip install 给我这个错误“无法打开文件&#39;pip&#39;:[Errno 2]没有这样的文件或目录” - pip install gives me this error “can't open file 'pip': [Errno 2] No such file or directory” Docker中的pip不能打开文件&#39;/ usr / local / bin / pip&#39;:[Errno 13]权限被拒绝 - pip in docker getting can't open file '/usr/local/bin/pip': [Errno 13] Permission denied 使用open()时的Python FileNotFoundError - Python FileNotFoundError when using open()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM