ansible / galaxy

Legacy Galaxy still available as read-only on https://old-galaxy.ansible.com - looking for the new galaxy -> https://github.com/ansible/galaxy_ng
Apache License 2.0
849 stars 330 forks source link

cannot import name 'CollectionRequirement' from 'ansible.galaxy.collection' #2904

Open isuftin opened 2 years ago

isuftin commented 2 years ago

Bug Report

SUMMARY

When running the ansible-galaxy command, I get the following:

ERROR! Unexpected Exception, this is probably a bug: cannot import name 'CollectionRequirement' from 'ansible.galaxy.collection' (/usr/local/lib/python3.9/site-packages/ansible/galaxy/collection/__init__.py)
the full traceback was:

Traceback (most recent call last):
  File "/usr/local/bin/ansible-galaxy", line 92, in <module>
    mycli = getattr(__import__("ansible.cli.%s" % sub, fromlist=[myclass]), myclass)
  File "/usr/local/lib/python3.9/site-packages/ansible/cli/galaxy.py", line 24, in <module>
    from ansible.galaxy.collection import (
ImportError: cannot import name 'CollectionRequirement' from 'ansible.galaxy.collection' (/usr/local/lib/python3.9/site-packages/ansible/galaxy/collection/__init__.py)

This is happening against installation of Ansible 2 and 3.

4 and 5 seem to work ok.

Alpine Linux Docker image:

cat /etc/os-release
NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.15.0
PRETTY_NAME="Alpine Linux v3.15"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://bugs.alpinelinux.org/"

python --version Python 3.9.11

STEPS TO REPRODUCE

requirements.txt:

ansible==2.10.7
    # via -r ./requirements.in
ansible-base==2.10.17
    # via ansible
ansible-compat==2.0.2
    # via
    #   ansible-lint
    #   molecule
    #   molecule-docker
ansible-core==2.12.3
    # via ansible-lint
ansible-lint==6.0.0
    # via -r ./requirements.in
arrow==1.2.2
    # via jinja2-time
attrs==21.4.0
    # via pytest
awxkit==20.0.1
    # via -r ./requirements.in
bcrypt==3.2.0
    # via paramiko
binaryornot==0.4.4
    # via cookiecutter
boto==2.49.0
    # via -r ./requirements.in
boto3==1.21.21
    # via -r ./requirements.in
botocore==1.24.21
    # via
    #   boto3
    #   s3transfer
bracex==2.2.1
    # via wcmatch
cerberus==1.3.2
    # via molecule
certifi==2021.10.8
    # via requests
cffi==1.15.0
    # via
    #   bcrypt
    #   cryptography
    #   pynacl
chardet==4.0.0
    # via binaryornot
charset-normalizer==2.0.12
    # via requests
click==8.0.4
    # via
    #   click-help-colors
    #   cookiecutter
    #   molecule
click-help-colors==0.9.1
    # via molecule
commonmark==0.9.1
    # via rich
cookiecutter==1.7.3
    # via molecule
cryptography==36.0.2
    # via
    #   ansible-base
    #   ansible-core
    #   paramiko
distro==1.7.0
    # via selinux
docker==5.0.3
    # via molecule-docker
enrich==1.2.7
    # via
    #   ansible-lint
    #   molecule
flake8==4.0.1
    # via -r ./requirements.in
idna==3.3
    # via requests
iniconfig==1.1.1
    # via pytest
jinja2==3.0.3
    # via
    #   ansible-base
    #   ansible-core
    #   cookiecutter
    #   jinja2-time
    #   molecule
    #   molecule-vagrant
jinja2-time==0.2.0
    # via cookiecutter
jmespath==1.0.0
    # via
    #   boto3
    #   botocore
markupsafe==2.1.1
    # via jinja2
mccabe==0.6.1
    # via flake8
molecule==3.6.1
    # via
    #   -r ./requirements.in
    #   molecule-docker
    #   molecule-ec2
    #   molecule-vagrant
molecule-docker==1.1.0
    # via -r ./requirements.in
molecule-ec2==0.4
    # via -r ./requirements.in
molecule-vagrant==1.0.0
    # via -r ./requirements.in
packaging==21.3
    # via
    #   ansible-base
    #   ansible-core
    #   ansible-lint
    #   molecule
    #   pytest
paramiko==2.10.2
    # via molecule
pathspec==0.9.0
    # via yamllint
pluggy==1.0.0
    # via
    #   molecule
    #   pytest
poyo==0.5.0
    # via cookiecutter
py==1.11.0
    # via pytest
pycodestyle==2.8.0
    # via flake8
pycparser==2.21
    # via cffi
pyflakes==2.4.0
    # via flake8
pygments==2.11.2
    # via rich
pynacl==1.5.0
    # via paramiko
pyparsing==3.0.7
    # via packaging
pytest==7.1.0
    # via pytest-testinfra
pytest-testinfra==6.6.0
    # via -r ./requirements.in
python-dateutil==2.8.2
    # via
    #   arrow
    #   botocore
python-slugify==6.1.1
    # via cookiecutter
python-vagrant==0.5.15
    # via molecule-vagrant
pyyaml==6.0
    # via
    #   ansible-base
    #   ansible-compat
    #   ansible-core
    #   ansible-lint
    #   awxkit
    #   molecule
    #   molecule-ec2
    #   molecule-vagrant
    #   yamllint
requests==2.27.1
    # via
    #   awxkit
    #   cookiecutter
    #   docker
    #   molecule-docker
resolvelib==0.5.4
    # via ansible-core
rich==12.0.0
    # via
    #   ansible-lint
    #   enrich
    #   molecule
ruamel-yaml==0.17.21
    # via ansible-lint
ruamel-yaml-clib==0.2.6
    # via ruamel-yaml
s3transfer==0.5.2
    # via boto3
selinux==0.2.1
    # via molecule-vagrant
six==1.16.0
    # via
    #   bcrypt
    #   cookiecutter
    #   paramiko
    #   python-dateutil
subprocess-tee==0.3.5
    # via ansible-compat
text-unidecode==1.3
    # via python-slugify
tomli==2.0.1
    # via pytest
urllib3==1.26.9
    # via
    #   botocore
    #   requests
wcmatch==8.3
    # via ansible-lint
websocket-client==1.3.1
    # via docker
yamllint==1.26.3
    # via
    #   -r ./requirements.in
    #   ansible-lint

The above is created from pip-compile against:

boto3
boto
ansible<3
ansible-lint
molecule
molecule-vagrant
molecule-docker
molecule-ec2
pytest-testinfra
flake8
yamllint
awxkit

Dockerfile:

FROM python:3.9-alpine

RUN apk add --no-cache \
    libffi-dev~=3.4.2 \
    python3-dev~=3.9 \
    openssl-dev~=1.1.1 \
    make~=4.3 \
    cargo~=1.56

COPY requirements.txt .

RUN pip install -r requirements.txt
EXPECTED RESULTS

No errors

ACTUAL RESULTS
ERROR! Unexpected Exception, this is probably a bug: cannot import name 'CollectionRequirement' from 'ansible.galaxy.collection' (/usr/local/lib/python3.9/site-packages/ansible/galaxy/collection/__init__.py)
the full traceback was:

Traceback (most recent call last):
  File "/usr/local/bin/ansible-galaxy", line 92, in <module>
    mycli = getattr(__import__("ansible.cli.%s" % sub, fromlist=[myclass]), myclass)
  File "/usr/local/lib/python3.9/site-packages/ansible/cli/galaxy.py", line 24, in <module>
    from ansible.galaxy.collection import (
ImportError: cannot import name 'CollectionRequirement' from 'ansible.galaxy.collection' (/usr/local/lib/python3.9/site-packages/ansible/galaxy/collection/__init__.py)
nickkeyzer commented 2 years ago

I just ran into this issue as well. Based on your pip packages, it looks like you're specifically installing Ansible 2.10 as well as ansible-lint. Ansible-lint just released v6.0.0 which introduced a new dependency on ansible-core 2.12+. You cannot have a difference in version between ansible-core and ansible-base. This is what's causing the issue.

So to fix your issue you'll either need to upgrade to Ansible 2.12+ or lock ansible-lint to v5.4.x.

Hope that helps. Good luck.

DrPsychick commented 1 year ago

What fixed it for me, using venv, was reinstalling Ansible after I have installed it via the requirements. I don't know why, but this seems to fix it.

pip3 install -r requirements.txt
# ansible-galaxy fails
pip3 install --force 'ansible==5.2.0'
# ansible-galaxy succeeds