0
import os
import sys

#run get-pip.py
os.system("python get-pip.py")

#try to import pip
try:
    __import__('pip')
except ImportError:
    input('Could not install pip, please enter any key to quit this window.')
    sys.exit()

#install selenium
def install(package):
    pip.main(['install', package])

if __name__ == '__main__':
    install('selenium')

When I run this script I get:

NameError: name 'pip' is not defined

It's like __import__('pip') doesn't import pip, how can I make it import pip?

When I ran this code it installed pip fine, also the try didn't throw any exception. I get this error when it tries to install selenium, because pip doesn't get imported in the try for some reason I think.

4
  • Why wouldn't you just use import pip? Commented May 29, 2016 at 10:48
  • @DanielRoseman I want to make sure whether pip is installed or not, if it isn't installed then it shouldn't try to install Selenium through pip. Commented May 29, 2016 at 10:50
  • But that has nothing to do with it. import pip would still raise ImportError if pip was not installed. The only reason to use __import__() is if you're trying to import something dynamically, which you're not. Commented May 29, 2016 at 10:54
  • @DanielRoseman, thanks alot that fixed it for me. Make it as an answer instead of a comment so I can mark it as selected. Commented May 29, 2016 at 10:56

1 Answer 1

1

You should use the standard import pip unless you are trying to define the module to import dynamically. It will still raise ImportError if pip is not installed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.