Is it possible to install python libraries with pip programmatically? - python-3.x

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?

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.

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>")

Use Following to install lib. programmatically.
import pip
try:
pip.main(["install", "pandas"])
except SystemExit as e:
pass

Related

Python Packages Not Working From Same Directory As Working Packages

I am using Python38.
I am trying to create a script to create/modify google sheets. So I came across several articles detailing how to accomplish this: article1, article2, article3.
Essentially it boils down to using two python packages: GSPREAD and OAUTH2CLIENT.
The problem is trying to import either of these returns the Module Not Found Error.
Running this:
import sys
print(sys.path)
I see the site-packages directory is returned in the list.
I have many other packages installed in the same directory that do work (e.g. bs4, Django, Flask, matplotlib, etc, etc), so why are these two packages not being found? Does the PATH variable need to be updated?
Thanks in advance!!
So I failed to mention in the question that I was trying to do the pip install from the command line (cmd on windows). Which successfully installed the packages, but did not allow visibility to the rest of the python environments.
The way to get this to work was to run the pip install command from the Anaconda Prompt (Anaconda's command line).

Python3 not recogninzing face_recognition as a module

I am trying to use the implement the face_recognition module, so far without success. When I try to run it with an import in python it just says that there is no such module, however when i just try to run face_recognition on it's own it works just fine.
Case 1: Same directory name
Maybe your main folder name is face_recognition. If that's the case, then try to change it because if you try to import it, the python will import it from your main folder rather than importing it from the package folder.
Case 2: Permission
If you're using Linux, then make sure that the package folder is accessible. To verify that this the issue, try to run the program with elevated privileges using sudo command. You can try using the --user flag with pip (pip install face_recognition --user).
Case 3: Environment variables
It might be the case that your environment variables are not updated. May reference to the package folder is not present. That's why your python interpreter is unable to find the package. For path details:
import sys
print(sys.path)
Using this, you will get an idea of whether your interpreter searches for the package or not.

How to use system() command with administrator properties?

Say I have a simple program: (module being some module to import)
from os import system
try:
import module
except:
system('py -m pip install module')
import module
My problem is, the module that i need to install needs administrator privileges.
How do I launch py in administrator?
I have tried things like:
system('py',adminstrator), and stupid stuff like that hoping one of them to work, but to no avail.
Thanks!
You can follow the instructions here to create a sudo.cmd file, make it available in your system path, and then you can gain Administrator privileges in your Python script with:
system('sudo py -m pip install module')

PIP installed pywin32 service fails: Incorrect function

Running Python 3.6.5, PyWin32 223.
After install I have included the the C:\Python32\lib\win32 folder in path.
I am just running the normal testService shell that seems to be all over the internet. Can be found below.
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
class AppServerSvc (win32serviceutil.ServiceFramework):
_svc_name_ = "TestService"
_svc_display_name_ = "Test Service"
def __init__(self,args):
win32serviceutil.ServiceFramework.__init__(self,args)
self.hWaitStop = win32event.CreateEvent(None,0,0,None)
socket.setdefaulttimeout(60)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_,''))
self.main()
def main(self):
pass
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(AppServerSvc)
I can "compile" and install the service fine, as soon as I run it I get an error to check the system logs. When I do I get the following error.
The Test Service Process service terminated with the service-specific error Incorrect function..
I have not been able to find any help through google from this decade. I am new to the library and the only help I found was to add its lib to the path. If its a path error, its no longer that. Anyone have any ideas?
Thanks in advance!
It turns out that it was a permission issue. I request root for 30 seconds and worked like a charm. Just FYI if anyone is having this problem.
In my case the problem was in the way I run the python module. Instead of executing the Python script, I use
$ python -m module
This works correctly when running in the appropriate directory but when running as a service, the module could not be found. So the solution if executing modules directly with Python is to pip install the module so that it can be found by the service.
In my case was due to the fact that the python script I was trying to run as a service wasn't in the same drive of the python installation. Also check this useful answer.

Issues while installing utilities module in python

Am trying to install utilities module for both python and anaconda environment.I have python 3 in my Mac.Here is the error I get.
pip install utilities
Could not find a version that satisfies the requirement utilities (from versions: )
No matching distribution found for utilities
Please help :(
If you are trying the ML example from here, then please copy the utilities.py from chapter 2 in your python 3 in Lib directory and you will be able to use the utility module.
Please try the below:
pip install data-utilities
You can find more on the Python Package Index - data-utilities website.
There is no module named utilities in Python,I believe you have encountered importing a file called utilities.py by the line import utilities. Showing the full source code might help. Additionally, check the gitgub repo of your source code for a file called utilities.py and copy it to your execution folder. If you are talking about python-utils, check this link.
Python Utilities contains many standard utility modules to solve common problems. They are :
File System -- os, os.path, shutil
Running External Processes -- commands
Exceptions
HTTP -- urllib and urlparse
Check This link
If this doesn't solve your issue, try installing the utilities-package using :
pip install utilities-package

Resources