ImportError: cannot import name ‘constants’

Please fill out the fields below so we can help you better. Note: you must provide your domain name to get help. Domain names for issued certificates are all made public in Certificate Transparency logs (e.g. https://crt.sh/?q=example.com), so withholding your domain name here does not increase secrecy, but only makes it harder for us to provide help.

My domain is: blog.tiga.tech

I ran this command: certbot plugins

It produced this output:

An unexpected error occurred:
ImportError: cannot import name 'constants' from 'certbot' (/usr/local/lib/python3.7/dist-packages/certbot/__init__.py)
Please see the logfile '/tmp/tmp1n9a2w7d/log' for more details.

My web server is (include version): NGINX 1.18.0

The operating system my web server runs on is (include version): Debian 10

My hosting provider, if applicable, is:

I can login to a root shell on my machine (yes or no, or I don’t know): yes

I’m using a control panel to manage my site (no, or provide the name and version of the control panel): no

The version of my client is (e.g. output of certbot --version or certbot-auto --version if you’re using Certbot): Can’t run certbot commands.

/tmp/tmpzej6726k/log contains:

2020-07-20 17:12:12,741:DEBUG:certbot._internal.log:Exiting abnormally:
Traceback (most recent call last):
  File "/usr/local/bin/certbot", line 10, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.7/dist-packages/certbot/main.py", line 15, in main
    return internal_main.main(cli_args)
  File "/usr/local/lib/python3.7/dist-packages/certbot/_internal/main.py", line 1323, in main
    plugins = plugins_disco.PluginsRegistry.find_all()
  File "/usr/local/lib/python3.7/dist-packages/certbot/_internal/plugins/disco.py", line 215, in find_all
    plugin_ep = PluginEntryPoint(entry_point)
  File "/usr/local/lib/python3.7/dist-packages/certbot/_internal/plugins/disco.py", line 56, in __init__
    self.plugin_cls = entry_point.load()
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2411, in load
    return self.resolve()
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 2417, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "/usr/lib/python3/dist-packages/certbot_nginx/configurator.py", line 16, in <module>
    from certbot import constants as core_constants
ImportError: cannot import name 'constants' from 'certbot' (/usr/local/lib/python3.7/dist-packages/certbot/__init__.py)
2020-07-20 17:12:12,742:ERROR:certbot._internal.log:An unexpected error occurred:
2020-07-20 17:12:12,742:ERROR:certbot._internal.log:ImportError: cannot import name 'constants' from 'certbot' (/usr/local/lib/python3.7/dist-packages/certbot/__init__.py)
$ apt show python3-certbot-nginx
Package: python3-certbot-nginx
Version: 0.31.0-1
$ apt show certbot
Package: certbot
Version: 0.31.0-1

I installed certbot using these instructions https://certbot.eff.org/lets-encrypt/debianbuster-nginx
I’ve also installed the following certbot plugins using pip3:
certbot-plugin-gandi
certbot-dns-cloudflare

I saw this issue on a production Ubuntu 16.04 install. After some time troubleshooting I decided to try it on a brand new Debian 10 install, since I wanted to move on from Ubuntu 16.04 anyway.
I was able to reproduce this issue on a completely clean Debian 10 install after installing NGINX, certbot, python3-certbot-nginx, certbot-dns-cloudflare, and certbot-plugin-gand.

1 Like

It’s the installation of packages from both apt (0.31.0) and from pip (>=1.0) which causes this.

Basically, you have a post-1.0 version of the main certbot module in /usr/local/lib/python3.7 (pip-installed), where the constants module is in a new location compared to where it was in the 0.31.0 release. The Certbot nginx plugin (0.31.0) is still looking for the constants module in the pre-1.0 location, and when it can’t find it in the pip-installed certbot module, it crashes.

In a fresh environment, you should be able to install these packages from apt: certbot python3-certbot-nginx python3-certbot-dns-cloudflare, and then install the Gandi plugin from pip3.

Why can we install the Gandi DNS plugin from pip safely and not the Cloudflare one? It comes down to the Cloudflare plugin (all the official DNS plugins, in fact) having a dependency on certbot>=1.5.1, which ends up pulling a new version of the certbot module, because your existing 0.31.0 one can’t satisfy it. The Gandi plugin, on the other hand, has no such version constraint, and will happily work with many common versions of Certbot.

A bit complicated, but tl;dr; install everything from apt except for the Gandi plugin.

4 Likes

Bingo. Thanks!

$ certbot plugins

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* certbot-plugin-gandi:dns
Description: Obtain certificates using a DNS TXT record (if you are using Gandi
for DNS).
Interfaces: IAuthenticator, IPlugin
Entry point: dns = certbot_plugin_gandi.main:Authenticator

* dns-cloudflare
Description: Obtain certificates using a DNS TXT record (if you are using
Cloudflare for DNS).
Interfaces: IAuthenticator, IPlugin
Entry point: dns-cloudflare =
certbot_dns_cloudflare.dns_cloudflare:Authenticator

* nginx
Description: Nginx Web Server plugin
Interfaces: IAuthenticator, IInstaller, IPlugin
Entry point: nginx = certbot_nginx.configurator:NginxConfigurator

* standalone
Description: Spin up a temporary webserver
Interfaces: IAuthenticator, IPlugin
Entry point: standalone = certbot.plugins.standalone:Authenticator

* webroot
Description: Place files in webroot directory
Interfaces: IAuthenticator, IPlugin
Entry point: webroot = certbot.plugins.webroot:Authenticator
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.