3

Let me explain what I want to do.

The list of libraries I want installed is listed in a .txt file.

My script reads the list from the file sequentially, and if the script isn't installed, it installs it via pip, or if it is already installed, checks the version and updates it if necessary.

I googled it up but didn't find how to do that. Can you offer any help or guidance?

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
E.Laemas Kim
  • 87
  • 1
  • 8
  • Because there are many ways to do this. E.g. a. using pip freeze to get all the module used, b. use easyinstall and put required module inside setup.py c. Use packaging and environment tools like pipenv. – mootmoot Dec 02 '19 at 17:18
  • 1
    Does this answer your question? [Installing python module within code](https://stackoverflow.com/questions/12332975/installing-python-module-within-code) – David Z May 24 '20 at 07:04

3 Answers3

2

Yes, you can. Python module os does support running script programmatically. Since I don't know how your file structure looks like, I guess you can read the file and run the script sequentially.

import os

os.system("pip install <module>")
curiouscupcake
  • 1,157
  • 13
  • 28
2

Yes you can. Try this, here is an example of one module which is hard coded

import os
import subprocess
import sys

get_pckg = subprocess.check_output([sys.executable, '-m', 'pip', 'freeze'])
installed_packages = [r.decode().split('==')[0] for r in get_pckg.split()]
required_packeges = ['shopifyAPI']     // Make a change here to fetch from file
for packg in required_packeges:
    if packg in installed_packages:
        pass
    else:
        print('installing package')
        os.system('pip install ' + packg)

First i will fetch all installed modules and then i will check my required module is installed or not if not then it will install it.

Adam Strauss
  • 1,889
  • 2
  • 15
  • 45
1

Use Following to install lib. programmatically.

import pip

try:
    pip.main(["install", "pandas"])
except SystemExit as e:
    pass
Rudra shah
  • 804
  • 8
  • 10