15

EDIT: The computer in question was a client machine with restrictions on what software could be installed. I'm unsure if that may have been a cause of the issue or if the client's IT department gave the machine a corrupted version of pip. The recommended answers below probably would have worked but were blocked by the company's IT department and required admin login to do. I have since left that project and hope to avoid similar situations.

I'm attempting to install a WHL file

While attempting to run:

import pip
my_path = <a path to the WHL file>
pip.main(['install', my_path])

I received an Attribute error:

'module' object has no attribute 'main'

I ran help(pip) and

__main__ 

was listed as a package content.

I'm running Python 3.4 in the console.

DCUFan7
  • 385
  • 1
  • 3
  • 10

5 Answers5

26

they made a refactoring. you can support both 9 and 10 pip by using:

try:
    from pip import main as pipmain
except:
    from pip._internal.main import main as pipmain

and then use pipmain as you used pip.main. for example

pipmain(['install', "--upgrade", "pip"])
pipmain(['install', "-q", "package"])
11

easy_install --upgrade pip worked for me.

coding_idiot
  • 13,526
  • 10
  • 65
  • 116
9

My issue was related to my IDE (PyCharm). older versions of PyCharm does not support pip v10. Upgrading PyCharm solved it for me.

Anders Larsen
  • 600
  • 7
  • 13
1

For more recent versions of pip (pip>=10.0.0), the functionality described in the other answers will no longer work. I recommend running the pip with subprocess as follows:

import subprocess
import sys

my_path = <a path to the WHL file>
command_list = [sys.executable, "-m", "pip", "install", my_path]
with subprocess.Popen(command_list, stdout=subprocess.PIPE) as proc:
    print(proc.stdout.read())

This solution uses the current python executable to run the pip command as a commandline command. It was inspired by the solution mentioned here.

Floyd
  • 2,252
  • 19
  • 25
1

From pip 20.0.0, it's:

from pip._internal.cli.main import main as pipmain
bryant1410
  • 5,540
  • 4
  • 39
  • 40