Jump to content
Usr6

[Python] Self install missing modules

Recommended Posts

Posted

Poate fi inclus la inceputul oricarui script python ce utilizeaza module care nu sunt instalate default. Pentru instalarea modulelor lipsa foloseste pip. Momentan disponibil doar pentru sistemul de operare windows & python 2.7

import sys

import os

#######################################

# RST Self installing missing modules #

# Usr6 #

#######################################

#pentru ca uneori numele modulului difera de numele pachetului:

librarie = {"modul":"nume pachet", "bs4":"beautifulsoup4"}

def install_module(e):

module = str(e).split()[-1]

raw_input("Lipseste modulul %s, apasa Enter pentru a incerca instalarea lui automata..." %module)

if os.name == "nt":

if not os.path.isfile('C:\\Python27\\Scripts\\pip.exe'):

sys.exit("N-ai pip, n-ai noroc. Il gasesti aici: https://pypi.python.org/pypi/pip")

os.system ('C:\\Python27\\Scripts\\pip.exe install "%s"' %(librarie[module]))

#restarting script

os.system('echo restarting... && %s "%s"' %(sys.executable, sys.argv[0]))

sys.exit()

elif os.name =="linux":

sys.exit('%s ..."inca" NU, trebuie sa instalezi manual pachetul %s \ngoogle is your friend' %(os.name, librarie[module]))

else:

sys.exit('%s ..."inca" NU, trebuie sa instalezi manual pachetul %s \ngoogle is your friend' %(os.name, librarie[module]))

try:

#import module externe

from bs4 import BeautifulSoup

except Exception as d41d8cd98f00b204e9800998ecf8427e:

install_module(d41d8cd98f00b204e9800998ecf8427e)

#your script start here

print """

### ### ###

# # # #

### ### #

## # #

# # ### #forums"""

raw_input("Press Enter/Return to exit...")

sys.exit()

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...