Getting error No module named 'pip._internal.download' and ModuleNotFoundError: No module named 'pip.download'

When I run the below command to build my package

python3 -m build

Creating virtualenv isolated environment...
Installing packages in isolated environment... (setuptools >= 40.8.0, wheel)
Getting build dependencies for sdist...

I am getting this error:

    Traceback (most recent call last):
      File "<string>", line 17, in <module>
    ModuleNotFoundError: No module named 'pip._internal.download'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/home/ciasto/.local/lib/python3.10/site-packages/pyproject_hooks/_in_process/_in_process.py", line 353, in <module>
        main()
      File "/home/ciasto/.local/lib/python3.10/site-packages/pyproject_hooks/_in_process/_in_process.py", line 335, in main
        json_out['return_val'] = hook(**hook_input['kwargs'])
      File "/home/ciasto/.local/lib/python3.10/site-packages/pyproject_hooks/_in_process/_in_process.py", line 287, in get_requires_for_build_sdist
        return hook(config_settings)
      File "/tmp/build-env-h_1fvjgg/lib/python3.10/site-packages/setuptools/build_meta.py", line 328, in get_requires_for_build_sdist
        return self._get_build_requires(config_settings, requirements=[])
      File "/tmp/build-env-h_1fvjgg/lib/python3.10/site-packages/setuptools/build_meta.py", line 295, in _get_build_requires
        self.run_setup()
      File "/tmp/build-env-h_1fvjgg/lib/python3.10/site-packages/setuptools/build_meta.py", line 480, in run_setup
        super(_BuildMetaLegacyBackend, self).run_setup(setup_script=setup_script)
      File "/tmp/build-env-h_1fvjgg/lib/python3.10/site-packages/setuptools/build_meta.py", line 311, in run_setup
        exec(code, locals())
      File "<string>", line 19, in <module>
    ModuleNotFoundError: No module named 'pip.download'
    
    ERROR Backend subprocess exited when trying to invoke get_requires_for_build_sdist

I am using pip version 23.3.1

pip -V
pip 23.3.1 from /home/ciasto/.local/lib/python3.10/site-packages/pip (python 3.10)

and python 3.10

setuptools is 59.6.0

Python 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import setuptools
>>> setuptools
<module 'setuptools' from '/usr/lib/python3/dist-packages/setuptools/__init__.py'>
>>> setuptools.__version__
'59.6.0'

obviously, I want to know how can I fix this issue but I also want to tie the compatible version so that I do not fall into this issue again. ultimately I want to publish package to PyPi

What’s the content of your setup.py file?

try:
	from setuptools import setup
except ImportError:
	from distutils.core import setup

from distutils.core import setup
from os import path

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

install_reqs = list(parse_requirements("requirements.txt", session=PipSession()))

with open('requirements.txt', 'r') as reqh:
	install_reqs = reqh.readlines()


v = open(path.join(path.dirname(__file__), 'VERSION'))
VERSION = v.readline().strip()
v.close()

setup(
	name='mailer',
	version=VERSION,
	author='Vita Chaos',
	author_email='vitachaos@gmail.com',
	packages=['emaildiff', 'emaildiff/mail',],
	data_files = ['VERSION'],
	scripts=['scripts/git-maildiff'],
	url='https://github.com/vitachaos/mailer',
	license='BSD',
	description='Package to email color git diff',
	long_description=(open('README.md').read()),
	long_description_content_type='text/markdown',
	setup_requires=["setuptools>=58.0.4"],
	install_requires=install_reqs,
	entry_points={
	'console_scripts':
		['mailer=emaildiff.mailer_cmd:main']
				},
	 classifiers=[
			'Development Status :: 5 - Production/Stable',
			'Environment :: Console',
			'Intended Audience :: Developers',
			'Intended Audience :: System Administrators',
			'License :: OSI Approved :: BSD License',
			'Operating System :: Unix',
			'Programming Language :: Python',
			'Programming Language :: Python :: 3',
			'Programming Language :: Python :: 3.3',
			'Programming Language :: Python :: 3.4',
			'Programming Language :: Python :: 3.7',
			'Topic :: Software Development',
			'Topic :: Software Development :: Version Control',
			'Topic :: Utilities',
			],
	   platforms=['Unix', 'Darwin', 'Windows']
)

Where did you copy that code from? It’s very bad code. It imports the pip module, but pip is meant to be used as a command line tool, not as a library. This is also what the underscore means in from pip._internal.... When the name of a module starts with an underscore, it means that the module should be considered internal, and does not make any compatibility promises. In this case, it looks like the pip._internal.download module was removed from pip’s code, which is perfectly legit because it is an implementation detail and should never be used outside of pip itself.

Instead, if you really need to share the dependencies with a requirements.txt file, you could parse it yourself, but in 99% of cases, the right thing to do is to put the dependency list in the pyproject.toml or setup.py file (pyproject.toml is recommended, but since you’re using setup.py, it will be the install_requires keyword).

Also note that you will not be able to upload your package as-is to PyPI because the name mailer is already taken by this project.

3 Likes

it’s an old code I am resurrecting, and ontop I ran 2to3 to make quick fixes.

BTW, the Github account mentioned in that config is 404ing for me. Not just the repository, the entire account.

But yes, the simplest fix at this point is probably to just get rid of everything related to Pip, and also the redundant from distutils.core import setup - this is replacing the setuptools import (you don’t want that to happen) even if importing setuptools succeeds (which it certainly should). In fact, if we’re requiring a modern version of Setuptools then there is no point in trying to use distutils.core as a fallback; it will and should fail anyway.

I also fixed it to use a with block for the version info file, as well as the requirements.txt file; made the handling of the README file work the same way; and fixed indentation to use spaces consistently.

from setuptools import setup
from os import path

with open('requirements.txt') as reqh:
    install_reqs = reqh.readlines()

with open(path.join(path.dirname(__file__), 'VERSION')) as v:
    VERSION = v.readline().strip()

with open('README.md') as f:
    long_description = f.read()

setup(
    name='mailer',
    version=VERSION,
    author='Vita Chaos',
    author_email='vitachaos@gmail.com',
    packages=['emaildiff', 'emaildiff/mail',],
    data_files=['VERSION'],
    scripts=['scripts/git-maildiff'],
    url='https://github.com/vitachaos/mailer',
    license='BSD',
    description='Package to email color git diff',
    long_description=long_description,
    long_description_content_type='text/markdown',
    setup_requires=["setuptools>=58.0.4"],
    install_requires=install_reqs,
    entry_points={
        'console_scripts': ['mailer=emaildiff.mailer_cmd:main']
    },
    classifiers=[
        'Development Status :: 5 - Production/Stable',
        'Environment :: Console',
        'Intended Audience :: Developers',
        'Intended Audience :: System Administrators',
        'License :: OSI Approved :: BSD License',
        'Operating System :: Unix',
        'Programming Language :: Python',
        'Programming Language :: Python :: 3',
        'Programming Language :: Python :: 3.3',
        'Programming Language :: Python :: 3.4',
        'Programming Language :: Python :: 3.7',
        'Topic :: Software Development',
        'Topic :: Software Development :: Version Control',
        'Topic :: Utilities',
    ],
    platforms=['Unix', 'Darwin', 'Windows']
)

If there’s still a problem then we’ll need to look at the requirements.txt next.

(You’ll probably also want to try to test the code with various minor versions of Python 3, and update the related trove classifiers.)

2 Likes