pip.main install fails with 'module' object has no attribute 'main'
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
I had the same issue and just running the below command solved it for me:
The short answer is don't do this. Use
setup.pyor a straight import statement.
pipaffects the whole environment. Depending on who is running this and why, they may or may not want to install
requestsin the environment they're running your script in. It could be a nasty surprise that running your script affects their python environment.
Installing it as a package (using
pip install) is a different matter. There are well-established ways to install other packages using
setup.py. It is also expected that installing a package will install its dependencies. You can read more in the python.org packaging tutorials
If your script has dependencies but people don't need to install it, you should tell them in the
requirements.txt. or simply include the import statement, and when they get the error, they'll know what to do. Let them control what environment installs which packages.