问题
I want to auto renew certificate via certbot, but not working, got error "ImportError: No module named cryptography"
# ./certbot-auto --dry-run
Error: couldn't get currently installed version for /opt/eff.org/certbot/venv/bin/letsencrypt: Traceback (most recent call last): File "/opt/eff.org/certbot/venv/bin/letsencrypt", line 7, in from certbot.main import main File "/opt/eff.org/certbot/venv/local/lib/python2.7/dist-packages/certbot/main.py", line 10, in import josepy as jose File "/opt/eff.org/certbot/venv/local/lib/python2.7/dist-packages/josepy/init.py", line 41, in from josepy.interfaces import JSONDeSerializable File "/opt/eff.org/certbot/venv/local/lib/python2.7/dist-packages/josepy/interfaces.py", line 7, in from josepy import errors, util File "/opt/eff.org/certbot/venv/local/lib/python2.7/dist-packages/josepy/util.py", line 7, in import OpenSSL File "/opt/eff.org/certbot/venv/local/lib/python2.7/dist-packages/OpenSSL/init.py", line 8, in from OpenSSL import crypto, SSL File "/opt/eff.org/certbot/venv/local/lib/python2.7/dist-packages/OpenSSL/crypto.py", line 12, in from cryptography import x509 ImportError: No module named cryptography
I tried to install cryptography, and got this result
sudo /opt/eff.org/certbot/venv/bin/pip install cryptography
You are using pip version 9.0.1, however version 19.2.3 is available. You should consider upgrading via the 'pip install --upgrade pip' command.
Then I tried this command pip install --upgrade pip
DEPRECATION: Python 2.7 will reach the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 won't be maintained after that date. A future version of pip will drop support for Python 2.7. More details about Python 2 support in pip, can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support Requirement already up-to-date: pip in /usr/lib/python2.7/dist-packages/pip-19.2.3-py2.7.egg (19.2.3)
How to get cryptography installed?
回答1:
Successfully installedI got the exact same error message after I ran certbot-auto and it upgraded to the new version.
Here's how I solve this in my case:
pip install cryptography
cd /opt/eff.org/certbot/venv/lib64/python2.7
mv site-packages site-packages.sav
ln -s dist-packages/ site-packages
It's because the letsencrypt's custom version of python is looking for the package under site-packages subfolder while it's empty in my case and the package is under dist-packages subfolder.
Note: If you see cryptography is successfully installed then no need to run 'pip install --upgrade pip' command and can disregard the warning information.
回答2:
I also ran into this same problem when trying to renew my certbot certificate on AWS EC2 instance. I started by installing cryptography, then getting the virtual environment to look in the right directory for it, then there was another package that it couldn't find, and all the responses from github are that they don't support these AWS instances. Soooooo, I resorted to this simple idea, and it worked perfectly:
cd /opt
sudo rm -R eff.org
cd ~
wget https://dl.eff.org/certbot-auto
chmod a+x certbot-auto
./certbot-auto renew --debug
That is, I deleted the old installation, and reinstalled it with the "renew" command. Worked perfectly. Your keys aren't stored in the directories that you delete.
来源:https://stackoverflow.com/questions/57891591/how-to-fix-importerror-no-module-named-cryptography