74

I am installing tweepy, but I am running into an error about pip.req. I have pip installed, but for some reason pip.req still can't be found. I did a bunch of research online and the most I could find was some issue about incompatibilities between zapo (?) and python 2.7 causing the same error for some other user. The discussion was unclear about how to solve the problem, though. Thanks!

$ python2 setup.py install
Traceback (most recent call last):
  File "setup.py", line 5, in <module>
    from pip.req import parse_requirements
ImportError: No module named pip.req
  • Looks like they wanted to remove it. I don't entirely agree with the decision, but here is the PR to remove that module from pip 10. https://github.com/pypa/pip/pull/4700 – Matt May 05 '18 at 16:01

6 Answers6

157

This is happening lately because of a change in pip 10.

The fix is pretty easy. You probably have something like:

from pip.req import parse_requirements

Change that to something like:

try: # for pip >= 10
    from pip._internal.req import parse_requirements
except ImportError: # for pip <= 9.0.3
    from pip.req import parse_requirements

That should do it.

mlissner
  • 17,359
  • 18
  • 106
  • 169
  • 9
    Why'd they not add backwards compatibility. A lot of packages will fail installing with this error. :( – Niklas R Apr 27 '18 at 19:31
  • 1
    Seems like they regretted that this was available, since they moved it to the private namespace of `_internals`. But yeah, not nice to take it away. – mlissner Apr 27 '18 at 22:17
  • @kororo, I think you're reading the date as "April 2016" instead of as 16 April 2018 (the current year). – mlissner May 07 '18 at 17:46
  • The pip developers did not ever intend to vend API, but to be a tool only. – uchuugaka Sep 15 '18 at 12:21
  • 1
    Being shockingly few people, they didn't have a lot of time and energy to build an amazing transition plan. It blows my mind that the HUGE Python community contributes so little to what the tiny group of pip developers do. – uchuugaka Sep 15 '18 at 12:22
  • Thank you, this was very usefull =D – cesargroening Nov 27 '18 at 21:53
55

I downgraded to pip to 9.0.3 and things worked for me. Command for downgrading pip is

python -m pip install pip==9.0.3
skvp
  • 1,940
  • 1
  • 20
  • 25
35

It looks like it would work if you had this code:

def parse_requirements(filename):
    """ load requirements from a pip requirements file """
    lineiter = (line.strip() for line in open(filename))
    return [line for line in lineiter if line and not line.startswith("#")]

Do this:

  1. create a directory pip/
  2. add an empty file pip/__init__.py
  3. add a file pip/req.py
  4. put the code above into pip/req.py:
  5. modify the line in setup.py

    reqs = install_reqs

hughdbrown
  • 47,733
  • 20
  • 85
  • 108
  • 2
    ^ Thanks! So I can understand - why is that? Why is it that I have to add in this code myself and what does the code above do? –  Aug 07 '14 at 22:05
  • 3
    The author of tweepy wrote a `setup.py`script that references files and directories that don't exist. I am telling you what the author probably meant. (Chances are, these files are on his hard drive but not added to the git repo. It happens.) This code reads a pip `requirements.txt` file and extracts the desired modules to install. Then `setup.py` actually installs the files. – hughdbrown Aug 07 '14 at 22:07
  • @hughdbrown what line should I modify and by what? – Alex Aug 19 '16 at 10:11
  • 1
    @AliIssa I would recommend placing the function in the setup.py file rather than importing it. See my solution below. – Matthew Frost Aug 19 '16 at 13:58
  • Hi @hughdbrown, can you tell me the location of setup.py file that your talking about. Im really new to django/python – Anish Apr 25 '18 at 12:43
  • 1
    It is not entirely clear where to add the line `reqs=install_reqs` in the `setup.py` file, so I am stuck with the same problem. – Landmaster Dec 31 '18 at 01:37
11

I had a very similar problem with Python 3.7 + pip 18.0:

Traceback (most recent call last):
  File "/usr/local/bin/pip-compile", line 7, in <module>
    from piptools.scripts.compile import cli
  File "/usr/local/lib/python3.7/site-packages/piptools/scripts/compile.py", line 11, in <module>
    from pip.req import InstallRequirement, parse_requirements
ModuleNotFoundError: No module named 'pip.req'

The solution was to upgrade pip-tools from 1.10 to 2.0:

pip install -U pip-tools
hdiogenes
  • 729
  • 7
  • 15
9

Instead of importing the function and potentially encountering more issues replace the contents of the setup.py with the following:

#!/usr/bin/env python
#from distutils.core import setup
import re, uuid
from setuptools import setup, find_packages

def parse_requirements(filename):
    """ load requirements from a pip requirements file """
    lineiter = (line.strip() for line in open(filename))
    return [line for line in lineiter if line and not line.startswith("#")]


VERSIONFILE = "tweepy/__init__.py"
ver_file = open(VERSIONFILE, "rt").read()
VSRE = r"^__version__ = ['\"]([^'\"]*)['\"]"
mo = re.search(VSRE, ver_file, re.M)

if mo:
    version = mo.group(1)
else:
    raise RuntimeError("Unable to find version string in %s." % (VERSIONFILE,))

install_reqs = parse_requirements('requirements.txt')
reqs = install_reqs

setup(name="tweepy",
      version=version,
      description="Twitter library for python",
      license="MIT",
      author="Joshua Roesslein",
      author_email="tweepy@googlegroups.com",
      url="http://github.com/tweepy/tweepy",
      packages=find_packages(exclude=['tests']),
      install_requires=reqs,
      keywords="twitter library",
      classifiers=[
          'Development Status :: 4 - Beta',
          'Topic :: Software Development :: Libraries',
          'License :: OSI Approved :: MIT License',
          'Operating System :: OS Independent',
          'Programming Language :: Python',
          'Programming Language :: Python :: 2',
          'Programming Language :: Python :: 2.6',
          'Programming Language :: Python :: 2.7',
          'Programming Language :: Python :: 3',
          'Programming Language :: Python :: 3.3',
          'Programming Language :: Python :: 3.4',
      ],
      zip_safe=True)

Notice the session argument has been removed from the parse_requirements call.

Matthew Frost
  • 578
  • 5
  • 13
3

I ran into same problem you have. To install pip you need to follow this https://pypi.python.org/pypi/setuptools once you get easy_install I installed pip first and then run the following command.

sudo easy_install pip
sudo python setup.py install

easy.

Bedi Egilmez
  • 1,494
  • 1
  • 18
  • 26