AttributeError: 'ParsedRequirement' object has no attribute 'req'

I came across a very similar problem with the same error message. It occurred when working with pip-tools / pip-compile / pip-sync. The problem was probably related to non-compatible versions of pip and pip-tools and was resolved by updating both pip and pip-tools.

Resolving these problems could remove the necessity of the workaround in the answer of Mehant Kammakomati.


Update:

* Please note that updating pip and pip-tools is not supported in my case. Then the workaround in my answer will help.

* If updating pip and pip-tools to compatible version is supported then refer to Gnnr's answer or Heapify's answer

I got the fix finally \o/

install_reqs = parse_requirements(requirements_path, session=False)

At first, I have inspected what install_reqs was on Travis by simply logging it and found that it was a list of ParsedRequirement objects. I also found that this class is defined in req_file.py. I have gone to check the source code for req_file.py here on GitHub. I found that there was no such attribute called req but instead it is requirement. So there were two versions of parse_requirements function so I handled this using a try and except block.

# Generator must be converted to list, or we will only have one chance to read each element, meaning that the first requirement will be skipped.
requirements = list(requirements) 
try:
    requirements = [str(ir.req) for ir in install_reqs]
except:
    requirements = [str(ir.requirement) for ir in install_reqs]

Now it is compatible with both the versions \0/


I know this is not directly related to the error in Docker, but I was getting the same error and came across this post when I Googled and would like to add my two cents -

This error is definitely related to pip, pip-tools and python versions not being compatible. I looked at the compatible versions here and matched my project with them and the error was resolved.

I'd say, try this first before you do anything else