How to Fix Python 2 Deprecation and Force docker-compose to Use Python 3


2 views

When trying to run docker-compose on a Debian 9 system, you might encounter this warning:

/usr/local/lib/python2.7/dist-packages/paramiko/transport.py:33: CryptographyDeprecationWarning: 
Python 2 is no longer supported by the Python core team. Support for it is now deprecated in cryptography, 
and will be removed in a future release.
  from cryptography.hazmat.backends import default_backend

This occurs because the system's default Python is version 2.7, while modern tools like docker-compose are moving to Python 3.

Checking the environment reveals:

# cat /etc/debian_version 
9.13
# python2 --version
Python 2.7.13
# python3 --version
Python 3.5.3

Instead of using Python 2's pip, install docker-compose with Python 3:

# apt-get install python3-pip
# pip3 install docker-compose

After installation, running docker-compose should now work without Python 2 deprecation warnings:

# docker-compose ps
Name   Command   State   Ports
------------------------------

Though you might see a different warning about Python 3.5, this is less critical than the Python 2 deprecation.

For production systems, consider these more robust approaches:

1. Using the official Docker installation script:

# curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# chmod +x /usr/local/bin/docker-compose

2. Installing via Docker's repositories:

# apt-get update
# apt-get install docker-compose-plugin

For development environments, consider using virtual environments:

# python3 -m venv compose-env
# source compose-env/bin/activate
(compose-env) # pip install docker-compose

This isolates your docker-compose installation from system Python packages.

If stuck with Python 2 systems temporarily, you can create a Python 3 alias:

# alias docker-compose='python3 $(which docker-compose)'

But this is not recommended as a long-term solution.


When trying to run docker-compose on a system with Python 2.7, you encounter several deprecation warnings and eventually a syntax error:

CryptographyDeprecationWarning: Python 2 is no longer supported by the Python core team
...
File "/usr/local/lib/python2.7/dist-packages/pyrsistent/_pmap.py", line 98
    ) from e
         ^
SyntaxError: invalid syntax

The root cause is that while your system has both Python 2.7 and Python 3.5 installed, the docker-compose package was installed using Python 2's pip. Many modern Python packages are dropping support for Python 2 entirely, and some syntax (like the from e exception handling) isn't even valid in Python 2.

Here's how to properly install docker-compose with Python 3 on Debian 9:

# First, ensure Python 3 pip is installed
sudo apt-get install python3-pip

# Then install docker-compose using pip3
sudo pip3 install docker-compose

# Verify installation
docker-compose --version

After this, running docker-compose ps should work, though you might see a different warning:

/usr/local/lib/python3.5/dist-packages/paramiko/transport.py:33: CryptographyDeprecationWarning: Python 3.5 support will be dropped in the next release of cryptography. Please upgrade your Python.

For more control over Python versions, consider these approaches:

# Option 1: Use virtual environments
python3 -m venv myenv
source myenv/bin/activate
pip install docker-compose

# Option 2: Use pyenv to manage Python versions
curl https://pyenv.run | bash
pyenv install 3.8.12
pyenv global 3.8.12
pip install docker-compose

If you want to avoid Python dependencies altogether:

# Download the standalone binary
sudo curl -L "https://github.com/docker/compose/releases/download/v2.2.3/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

# Make it executable
sudo chmod +x /usr/local/bin/docker-compose

# Verify
docker-compose --version

The warning about Python 3.5 being deprecated in cryptography suggests you should consider upgrading your system Python. On Debian 9, you can install newer Python versions from testing repositories or use pyenv as shown above.

To suppress the warning temporarily (not recommended for production):

export PYTHONWARNINGS="ignore::CryptographyDeprecationWarning"
docker-compose up