PyOpenSSL with ffi.from_buffer(b"") Error - python

Cannot import or even install anything using pip after installing pyopenssl. Getting the following error
***Traceback (most recent call last):
File "/usr/local/bin/pip3", line 11, in <module>
sys.exit(main())
File "/home/srnadmin/.local/lib/python3.5/site-packages/pip/_internal/cli/main.py", line 73, in main
command = create_command(cmd_name, isolated=("--isolated" in cmd_args))
File "/home/srnadmin/.local/lib/python3.5/site-packages/pip/_internal/commands/__init__.py", line 96, in create_command
module = importlib.import_module(module_path)
File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 986, in _gcd_import
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/srnadmin/.local/lib/python3.5/site-packages/pip/_internal/commands/install.py", line 24, in <module>
from pip._internal.cli.req_command import RequirementCommand
File "/home/srnadmin/.local/lib/python3.5/site-packages/pip/_internal/cli/req_command.py", line 15, in <module>
from pip._internal.index.package_finder import PackageFinder
File "/home/srnadmin/.local/lib/python3.5/site-packages/pip/_internal/index/package_finder.py", line 21, in <module>
from pip._internal.index.collector import parse_links
File "/home/srnadmin/.local/lib/python3.5/site-packages/pip/_internal/index/collector.py", line 12, in <module>
from pip._vendor import html5lib, requests
File "/home/srnadmin/.local/lib/python3.5/site-packages/pip/_vendor/requests/__init__.py", line 97, in <module>
from pip._vendor.urllib3.contrib import pyopenssl
File "/home/srnadmin/.local/lib/python3.5/site-packages/pip/_vendor/urllib3/contrib/pyopenssl.py", line 46, in <module>
import OpenSSL.SSL
File "/usr/local/lib/python3.5/dist-packages/OpenSSL/__init__.py", line 8, in <module>
from OpenSSL import crypto, SSL
File "/usr/local/lib/python3.5/dist-packages/OpenSSL/crypto.py", line 15, in <module>
from OpenSSL._util import (
File "/usr/local/lib/python3.5/dist-packages/OpenSSL/_util.py", line 152, in <module>
with ffi.from_buffer(b""):
TypeError: from_buffer() cannot return the address of the raw string within a bytes or unicode or bytearray object***
Can anyone please help me with this?

Got it to work
rm -rf /usr/local/lib/python3.5/dist-packages/OpenSSL/
pip3 uninstall pyopenssl
easy_install pyopenssl

It happened to me as well. (I'm running Python 3.7 on MacOS Catalina with Anaconda).
So apparently that's a problem with either cryptography module. Even pip uninstall wasn't working.
You could use this link for finding out the location of the cryptography package and delete it. Once deleted, just do pip install --upgrade cryptography and it will just work fine.
But in my case that wasn't working. So here's what I did:
Activate your env which has this problem. That could be done using conda activate <env_name> or source <path_to_env>/bin/activate.
(If there's no env, just skip this step)
Next execute where python or just conda info --envs if you're using anaconda. This should give you the path to the python executable. In my case it was ~/opt/anaconda3 as the output for conda info --envs.
Next just cd into that directory and then remove (or move it somewhere else in case you want a backup) the ~/opt/anaconda3/bin/python/cryptography folder. Or you could even do find . | grep cryptography after cding in the particular env folder to know the exact location of the package.
Next execute pip install --upgrade cryptography. In case that didn't work, you could try the same with the pyopenssl package.
Hope it helped.

Related

Unable to install pip modules or do anything related to pip caches

I uses PyCharm's terminal for the action below.
I was unable to install packages using pip. If I tried to install, it raises a error.
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Users\~\PycharmProjects\pythonProject\venv\Scripts\pip.exe\__main__.py", line 7, in <module>
File "C:\Users\~\PycharmProjects\pythonProject\venv\lib\site-packages\pip\_internal\cli\main.py", line 68, in main
command = create_command(cmd_name, isolated=("--isolated" in cmd_args))
File "C:\Users\~\PycharmProjects\pythonProject\venv\lib\site-packages\pip\_internal\commands\__init__.py", line 114, in create_command
module = importlib.import_module(module_path)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:\Users\~\PycharmProjects\pythonProject\venv\lib\site-packages\pip\_internal\commands\install.py", line 13, in <module>
from pip._internal.cache import WheelCache
ModuleNotFoundError: No module named 'pip._internal.cache'
Looking at the error, I found it's related to "caches", And I wrote a program which deletes all files which contains "cache" in it's name a few days ago since my computer is full of cache before.
My question is: Is it possible to add back the pip cache file back?
Looking forward to replys and answers.
Edit: Errors returned by using "pip install":
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 197, in _run_module_as_main
return _run_code(code, main_globals, None,
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Users\terence\PycharmProjects\pythonProject\venv\lib\site-packages\pip\__main__.py", line 31, in <module>
sys.exit(_main())
File "C:\Users\terence\PycharmProjects\pythonProject\venv\lib\site-packages\pip\_internal\cli\main.py", line 68, in main
command = create_command(cmd_name, isolated=("--isolated" in cmd_args))
File "C:\Users\terence\PycharmProjects\pythonProject\venv\lib\site-packages\pip\_internal\commands\__init__.py", line 114, in create_command
module = importlib.import_module(module_path)
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.9_3.9.3568.0_x64__qbz5n2kfra8p0\lib\importlib\__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "C:\Users\terence\PycharmProjects\pythonProject\venv\lib\site-packages\pip\_internal\commands\uninstall.py", line 9, in <module>
from pip._internal.cli.req_command import SessionCommandMixin, warn_if_run_as_root
File "C:\Users\terence\PycharmProjects\pythonProject\venv\lib\site-packages\pip\_internal\cli\req_command.py", line 15, in <module>
from pip._internal.cache import WheelCache
ModuleNotFoundError: No module named 'pip._internal.cache'
The error message "ModuleNotFoundError: No module named 'pip._internal.cache'" typically indicates that there is an issue with the version of pip that you have installed on your system. This error message occurs when the module 'pip._internal.cache' is not found in the version of pip that you are using.
To fix this error, you can try upgrading pip to the latest version by running the command:
pip install --upgrade pip
If the issue persists, you can try uninstalling and reinstalling pip using the command:
python -m pip uninstall pip
And then
python -m pip install pip
Alternatively, you can try installing the package using the following command:
python -m pip install package_name
It's also possible that you have multiple versions of python installed on your system, and the one you're using doesn't have pip package. In this case, you can try running the above command with the python version that has pip installed.
It's also a good practice to use virtual environments to keep the dependencies and packages isolated.
Please let me know if there's anything else I can help with.
As you python environment is broken, I'd recommend you to reinstall pip.
If that doesn't work, just reinstall python.
Also, better don't delete all files with a specific name-pattern.
You (as you see) just break programms with it.
Better delete all files in you temp folder (%temp% in windows)

pip install pyqtgraph complains about missing pip._internal.vcs.mercurial module

I have been using PyQt6 for a while without a problem. Now I'm trying to do the exercises from a textbook, using pyqtgraph. It does not seem to come bundled with PyQt6 because the import statement failed. I tried installing it with:
pip install pyqtgraph
I tried various combinations of sudo, pip, pip3 (pip points to pip3), python/python3.8/python3.9 -m pip install pyqtgraph (python links to python3). I forced a reinstall of pip from a curl and deleted old site-package versions from .local. I even did a git clone pyqtgraph and tried to build it from scratch. Nothing worked. I always get the same error:
Traceback (most recent call last):
File "/home/richard/.local/bin/pip", line 8, in <module>
sys.exit(main())
File "/home/richard/.local/lib/python3.9/site-packages/pip/_internal/cli/main.py", line 68, in main
command = create_command(cmd_name, isolated=("--isolated" in cmd_args))
File "/home/richard/.local/lib/python3.9/site-packages/pip/_internal/commands/__init__.py", line 109, in create_command
module = importlib.import_module(module_path)
File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/home/richard/.local/lib/python3.9/site-packages/pip/_internal/commands/install.py", line 14, in <module>
from pip._internal.cli.req_command import (
File "/home/richard/.local/lib/python3.9/site-packages/pip/_internal/cli/req_command.py", line 20, in <module>
from pip._internal.index.collector import LinkCollector
File "/home/richard/.local/lib/python3.9/site-packages/pip/_internal/index/collector.py", line 42, in <module>
from pip._internal.network.session import PipSession
File "/home/richard/.local/lib/python3.9/site-packages/pip/_internal/network/session.py", line 31, in <module>
from pip._internal.network.auth import MultiDomainBasicAuth
File "/home/richard/.local/lib/python3.9/site-packages/pip/_internal/network/auth.py", line 22, in <module>
from pip._internal.vcs.versioncontrol import AuthInfo
File "/home/richard/.local/lib/python3.9/site-packages/pip/_internal/vcs/__init__.py", line 7, in <module>
import pip._internal.vcs.mercurial
ModuleNotFoundError: No module named 'pip._internal.vcs.mercurial'
I also reinstalled mercurial.
I can't find any posts on pip._internal.vcs.mercurial, although there are plenty of posts on missing pip._internal, none of which helps.

Unable to install BeautifulSoup package for Python 3.10 using pip on Ubuntu 20.04

I was trying to install BeautifulSoup4 in order to learn web scraping. I was using pip to install bs4 package for Python 3.10 but I am unable to install it. Any help to resolve the below traceback error.
Note: I am using WSL2 Ubuntu 20.04
root#mascot:~# pip install bs4
Traceback (most recent call last):
File "/usr/bin/pip", line 11, in <module>
load_entry_point('pip==20.0.2', 'console_scripts', 'pip')()
File "/usr/lib/python3/dist-packages/pip/_internal/cli/main.py", line 73, in main
command = create_command(cmd_name, isolated=("--isolated" in cmd_args))
File "/usr/lib/python3/dist-packages/pip/_internal/commands/__init__.py", line 96, in create_command
module = importlib.import_module(module_path)
File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
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 "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "/usr/lib/python3/dist-packages/pip/_internal/commands/install.py", line 24, in <module>
from pip._internal.cli.req_command import RequirementCommand
File "/usr/lib/python3/dist-packages/pip/_internal/cli/req_command.py", line 15, in <module>
from pip._internal.index.package_finder import PackageFinder
File "/usr/lib/python3/dist-packages/pip/_internal/index/package_finder.py", line 21, in <module>
from pip._internal.index.collector import parse_links
File "/usr/lib/python3/dist-packages/pip/_internal/index/collector.py", line 12, in <module>
from pip._vendor import html5lib, requests
ImportError: cannot import name 'html5lib' from 'pip._vendor' (/usr/lib/python3/dist-packages/pip/_vendor/__init__.py)
use pip install beautifulsoup4 instead of bs4
Currently BS4/Beautifulsoup4 is not compatible with Python 3.10 (probably will working in future)
BS4 Release was in Sep 8, 2021
https://pypi.org/project/beautifulsoup4/#history
Python 3.10 realease in: October 04, 2021
https://devguide.python.org/#status-of-python-branches
I would assume compatibility issues until BS4 will update.
Until then everything should work with 3.9 so I would go with that.

my fixed pyOpenSSL version 19.0.0 suddenly broke

I have a Django app running on an old CentOS 7 box, and to have my own python3 environment there I'm using venv and pip. Today I rebuilt my environment, and suddenly, I had an ssl related backtrace.
[root#miketug1 teleworker]# vpython manage.py showmigrations
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/usr/lib/tug/env/lib64/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/usr/lib/tug/env/lib64/python3.6/site-packages/django/core/management/__init__.py", line 338, in execute
django.setup()
File "/usr/lib/tug/env/lib64/python3.6/site-packages/django/__init__.py", line 27, in setup
apps.populate(settings.INSTALLED_APPS)
File "/usr/lib/tug/env/lib64/python3.6/site-packages/django/apps/registry.py", line 108, in populate
app_config.import_models()
File "/usr/lib/tug/env/lib64/python3.6/site-packages/django/apps/config.py", line 202, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib64/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/etc/e-smith/web/django/teleworker/dashboard/models.py", line 9, in <module>
import teleworker.lib.common as commonlib
File "/etc/e-smith/web/django/teleworker/lib/common.py", line 35, in <module>
import requests
File "/usr/lib/tug/env/lib64/python3.6/site-packages/requests/__init__.py", line 95, in <module>
from urllib3.contrib import pyopenssl
File "/usr/lib/tug/env/lib64/python3.6/site-packages/urllib3/contrib/pyopenssl.py", line 46, in <module>
import OpenSSL.SSL
File "/usr/lib/tug/env/lib64/python3.6/site-packages/OpenSSL/__init__.py", line 8, in <module>
from OpenSSL import crypto, SSL
File "/usr/lib/tug/env/lib64/python3.6/site-packages/OpenSSL/SSL.py", line 664, in <module>
_lib.Cryptography_HAS_TLSEXT_HOSTNAME, "SNI not available"
AttributeError: module 'lib' has no attribute 'Cryptography_HAS_TLSEXT_HOSTNAME'
The versions of my modules are set in a requirements.txt file, which did not change. I was running pyOpenSSL 19.0.0, with Python 3.6. I upgraded to pyOpenSSL 20.0.0 and the problem went away, but I am confused as to how this could happen unless the 19.0.0 version on PyPI was changed, as I've used fixed versions of this module for some time now.
Can anyone explain what happened here? I'd like to avoid anything similar in the future. Thanks.
The root cause for this issue is the latest cryptography-3.3 package which was released 20 hours ago; unfortunately, the pyopenssl-19 has a weird requirement (cryptography >=2.3) hence the pip install automatically upgraded the cryptography to 3.3.
We have both pip and pip3 installed. So the following sequence is what fixed it for me:
sudo apt update; pip install -U cryptography; sudo apt remove python3-openssl -y; sudo apt autoremove; pip3 install -U cryptography;
I think I found it. I didn't have the cryptography module in my requirements.txt, so it was upgraded to the latest version without upgrading pyOpenSSL which was in my requirements.txt file. Seems like some kind of dependency should have prevented this, but I'm updating my requirements.txt to include everything now.
There's a new cryptography 3.3.1 which fixes the issue

Error in Hosting ReadTheDocs in house Server in python3

I am trying to install readthedocs in local system (Ubuntu 14.04 ) in python3 virtual env from the instructions given in this link
When I ran pip3 install -r requirements.txt , I got an error for Distutils2 . I removed that dependency as the distutils2 is no longer supported and assumed setuptools would suffice.
Running this command python manage.py migrate gave the below error :
Traceback (most recent call last):
File "manage.py", line 11, in <module>
execute_from_command_line(sys.argv)
File "/home/username/read_the_docs_env/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
utility.execute()
File "/home/username/read_the_docs_env/lib/python3.4/site-packages/django/core/management/__init__.py", line 312, in execute
django.setup()
File "/home/username/read_the_docs_env/lib/python3.4/site-packages/django/__init__.py", line 18, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/username/read_the_docs_env/lib/python3.4/site-packages/django/apps/registry.py", line 115, in populate
app_config.ready()
File "/home/username/read_the_docs_env/lib/python3.4/site-packages/django/contrib/admin/apps.py", line 22, in ready
self.module.autodiscover()
File "/home/username/read_the_docs_env/lib/python3.4/site-packages/django/contrib/admin/__init__.py", line 24, in autodiscover
autodiscover_modules('admin', register_to=site)
File "/home/username/read_the_docs_env/lib/python3.4/site-packages/django/utils/module_loading.py", line 74, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "/home/username/read_the_docs_env/lib/python3.4/importlib/__init__.py", line 109, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 2254, in _gcd_import
File "<frozen importlib._bootstrap>", line 2237, in _find_and_load
File "<frozen importlib._bootstrap>", line 2226, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 1200, in _load_unlocked
File "<frozen importlib._bootstrap>", line 1129, in _exec
File "<frozen importlib._bootstrap>", line 1471, in exec_module
File "<frozen importlib._bootstrap>", line 321, in _call_with_frames_removed
File "/home/username/Desktop/CurrentProjects/read_the_docs/checkouts/readthedocs.org/readthedocs/core/admin.py", line 10, in <module>
from readthedocs.core.views import SendEmailView
File "/home/username/Desktop/CurrentProjects/read_the_docs/checkouts/readthedocs.org/readthedocs/core/views/__init__.py", line 26, in <module>
from readthedocs.projects.tasks import remove_dir
File "/home/username/Desktop/CurrentProjects/read_the_docs/checkouts/readthedocs.org/readthedocs/projects/tasks.py", line 493
print "Sync Versions Exception: %s" % e.message
^
SyntaxError: Missing parentheses in call to 'print'
I understand from the above stacktrace that the code is in python2 and so print statement is different in python3 .
Does this mean that I have to install readthedocs in python2 virtualenv ?
Can't we host the docs of python3 projects in readthedocs in-house server ?
The read the docs code does not support Python 3 yet. The installation instructions explicitly say to use Python 2.7:
First, obtain Python 2.7 and virtualenv
However, it should still be possible to use your read the docs installation to host docs for Python 3 projects, since the instructions then say:
If you plan to import Python 3 project to your RTD then you’ll need to install Python 3 with virtualenv in your system as well.

Categories