Check errors/warnings while installing python modules from code using pip - python-3.x

I want to report those modules which failed to install by checking for any errors. Suppose I have a code with wrong or misspelled module-name. I want my code to print the module name for which installation failed due to some reason. For example:
import pip
pip.main(['install', 'someMisspelledModuleName==1.2.3'])
I have a dictionary from which I am reading such module names and their versions and installing it from my code itself in Python 3.4. When I am executing this script in Python Shell, it just gives red-coloured warning, which is not an error and hence I can't handle it.
Please tell me how to handle such cases?

pip.main(…) returns a status code, one of the predefined. Just check if it's SUCCESS or no:
import sys, pip
rc = pip.main(['install', 'someMisspelledModuleName==1.2.3'])
sys.exit(rc)
I just exit here using the result code.

Related

XGBoost module not found in site-packages. Imports but doesn't work. (mac OS)

I was able to install xgboost on my mac and have it so that I can import the module without any errors. However I cannot use it. When I try to import XGBRegressor. I get the following error :
cannot import name 'XGBRegressor' from 'xgboost' (unknown location).
Some of the support for this type of problem says that there may be more than one xgboost location, and that python is selecting the incorrect xgboost, however I have not been able to find how to fix this problem.
When I print the xgboost.__file__ location, it returns 'None'. However I know its in the site-packages folder in my python path.
I have python 3.9
Maybe this.
conda install -c conda-forge xgboost=0.6a2
Then, restart Spyder and re-run!

Scapy not working with Python 3.6. Import problems with Python?

I am trying to test a simple code that will inspect a .pcap file using scapy. My OS is Ubuntu 18.04 and my Python version is 3.6.
However when I do: from scapy.all import * I get the error message ModuleNotFoundError: No module named 'scapy.all'.
When I change this to be from scapy import *, I get an error later in my code when I try and use the scapy sendrecv sniff function. I get the error NameError: name 'sniff' is not defined. Note if I switch from sniff() to rdpcap() I get the same error but its now "rdpcap is not defined".
I've read through a bunch of previous stack overflow answers and nothing is helping. My python script name is "pcap_grapher.py", so the issue is not the script name being scapy.py.
When I run pip3 install scapy in the terminal I get the message Requirement already satisfied: scapy in /home/vic/.local/lib/python3.6/site-packages (2.4.4)
I've also tried running pip3 install --pre scapy[basic] as the docs recommend here: https://scapy.readthedocs.io/en/latest/installation.html but it didn't help.
Any advice would be greatly appreciated. I am super stuck.
First off, you will never be able to import sniff() from scapy, since it is within the submodule scapy.all. There are two ways I can think of to fix this:
Since you're on Ubuntu, try running sudo apt-get install python3-scapy and see if it lets you run from scapy.all import sniff now.
Check the path that python is currently pulling from by typing the following in terminal:
$ which scapy
/location-of-scapy-on-your-system
Then run
$ python
>>> import os
>>> scapy_path = "/location-of-scapy-on-your-system"
>>> if not scapy_path in os.sys.path: os.sys.path.append(scapy_path)
That should hopefully fix things. If not, it's possible you installed the wrong version of scapy: you can check this by running
$ python
>>> import scapy
>>> print(scapy.__version__)

Unable to use gitpython ImportError

Tried to run a script I wrote yesterday again today and ran into this:
ImportError: cannot import name 'Repo' from 'git' (/usr/local/lib/python3.7/site-packages/git/__init__.py)
I am at a complete loss. On a new computer so the only thing different install wise would have been that I installed pycharm. I am currently trying to run through bash shell on a mac. The exact code was running as excepted earlier, no code changes.
Things I have tried:
uninstalling/reinstalling python
uninstalling/reinstalling pip
uninstalling/reinstalling gitpython
Running on:
mac catalina
python version 3.7.6
pip version 20.0.1
As a side note, the script works as intended until the automated git push. Wondering if I should just make the os calls myself and not worry about this?
I really dont understand what I did/am doing wrong here.
EDIT:
Again, sorry as this is my first mac computer. I did a brew uninstall of python3 and reinstalled through the app store to 3.8.
ImportError: cannot import name 'is_cygwin_git' from partially initialized module 'git.util' (most likely due to a circular import) (/Library/Frameworks/Python.framework/
I know cygwin is for windows, but I figured I'd play along and trying a pip install pycygwin.
Install threw an error asking for cython, so I did another pip install and tried again. The pycygwin then complained gcc was missing so I did a brew install of gcc. With gcc installed and correctly on path, it still says it cant find it and exits with
build/cygwin/_cygwin.c:611:10: fatal error: 'sys/cygwin.h' file not found
#include <sys/cygwin.h>
^~~~~~~~~~~~~~
1 error generated.
error: command 'gcc' failed with exit status 1
Thinking I might just try a different package manager? Currently, an attempt to rerun the script yeilds
ImportError: cannot import name 'Repo' from partially initialized module 'git' (most likely due to a circular import)
To which I investigated but I shouldn't have any overlapping dependancies.
On the script throwing the error I'm using:
import csv
import yaml
import os
from git import Repo
and on the wrapper I made and imported, I'm using:
import subprocess
import re
Will update if I get any further on this, would love some suggestions.
EDIT:
Importing using just import git works throws a different error, like the python is trying to get itself?
ImportError: cannot import name '<file name>' from '<file name>'
If I change the file name and try to run it, it comes backs with:
ImportError: cannot import name '<old file name>' from '<old file name>'
***FIXED****
Uninstall of python through homebrew
Reinstall of python through mac app store
Uninstall/Reinstall of modules through pip
Saving the file under a new name and deleting the old one
Still have absolutely no idea why/how this happened but the above worked for me. If anyone knows why something like this can happen, I would love to know. Cheers.

Is it possible to install python libraries with pip programmatically?

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

RiveScript gives an error for python object macros

Is it possible to access the object macros internally in RiveScript? I'm bit confused with that.
I have defined the following script:
> object small python
return "4"
< object
+ small
- <call>small</call>
Which gives me an error when I enter small:
error: [ERR: Object Not Found]
make a space after the object name just like that
- <call>small </call>
The definition of the object seems to be ok, but you are probably running it from https://play.rivescript.com/ or https://www.rivescript.com/try, which only allow JavaScript or CoffeeScript (see the about page).
You can use rivescript-python to run the code, which you can install by:
pip install rivescript
Then put your rivescript in a file (.rive), for example, in helloworld.rive.
Then in python:
from rivescript import RiveScript
bot = RiveScript()
bot.load_directory('.') # set your directory where the .rive file(s) is/are
bot.sort_replies()
Now you can use bot.reply to get the answer for a certain input:
>>> bot.reply('localuser','small')
'4'
Edit: I have tested this code on Ubuntu 14.04 with Python 3.4.3 and Python 2.7.12 and rivescript 1.14.4.

Resources