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

Time:01-19

I am working on calling the Rest-API from Python. All the Rest-API I have tested using Postman and are working fine. But, While executing those using Python scripts I am facing certification error.

I am executing the python script from windows cmd.

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. 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 ?

CodePudding user response:

Maybe you can try to check the path where Python interpreter import packages.

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.

CodePudding user response:

This StackOverFlow question has the same issue and the solution provided worked for me.

Remove the following two files from your Python library folder (e.g., C:\Users<username>\AppData\Local\Programs\Python\Python310\Lib\site-packages if you are using Windows Python 3.10 and no 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.

  • Related