Do0msDay Do0msDay - 1 year ago 87
Python Question

Use Python module on the runtime

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.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download