pip.main install fails with 'module' object has no attribute 'main'

10,329

Solution 1

pip Developers do not recommend calling pip from within the program. And the pip.main() method has been removed from pip v10. As an alternative method it is recommended to execute pip in subprocess.

https://pip.pypa.io/en/stable/user_guide/?highlight=_internal#using-pip-from-your-program

try:
    import requests
except:
    import sys
    import subprocess
    subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'requests==2.0.1', 'PyYAML==3.11'])
    import requests

Solution 2

I had the same issue and just running the below command solved it for me:

easy_install pip

Solution 3

The pip.main function was moved, not removed by pip devs. The highest voted solution here is not good. Going from python -> shell -> python is not a good practice when you can just run the python code direct. Try from pip._internal import main then you can use that main function to execute your pip calls like before.

Share:
10,329
Sagar birla
Author by

Sagar birla

Updated on June 30, 2022

Comments

  • Sagar birla
    Sagar birla almost 2 years

    I am trying to install few of the python packages from within a python script and I am using pip.main(install) for that. Below is code snippet

    try:
        import requests
    except:
        import pip
        pip.main(['install', '-q', 'requests==2.0.1','PyYAML==3.11'])
        import requests
    

    I have tried using importing main from pip._internal and using pipmain instead of pip.main() but it did not help.

    I am on pip version 9.0.1 and python 2.7