python with yum api - installing package - python

Hello i'm using the following code to install package through yum api , using pythongs cript i need to build some installation based on this code , now its installed but im getting some errors
#!/usr/bin/python
import sys
import platform
import urllib2, urllib
import re
import yum
package="ntp"
print ("Installing ntp")
print ("#################")
yb=yum.YumBase()
searchlist=['name']
arg=['ntp']
matches = yb.searchGenerator(searchlist,arg)
for (package, matched_value) in matches :
if package.name == 'ntp' : yb.install(package)
yb.buildTransaction()
yb.processTransaction()
errors i got after installation is done
Running rpm_check_debug
Traceback (most recent call last):
File "./test.py", line 29, in <module>
yb.processTransaction()
File "/usr/lib/python2.6/site-packages/yum/__init__.py", line 4928, in processTransaction
self._doTestTransaction(callback,display=rpmTestDisplay)
File "/usr/lib/python2.6/site-packages/yum/__init__.py", line 5027, in _doTestTransaction
raise Errors.YumTestTransactionError, errstring
yum.Errors.YumTestTransactionError: Test Transaction Errors: package ntp is already installed
even when i removed the ntp and run the script again its give me this error msg after finished installation
plus i want to adjust the installation process , to check if the package is already installed then print its already install and process to next step in the code , else process the installation steps ,
any tips also for for if condition in correct way using yum api

Hi the previous answer didn't work the correct one is the following:
import yum
yb=yum.YumBase()
inst = yb.rpmdb.returnPackages()
installed=[x.name for x in inst]
packages=['bla1', 'bla2', 'bla3']
for package in packages:
if package in installed:
print('{0} is already installed'.format(package))
else:
print('Installing {0}'.format(package))
kwarg = {
'name':package
}
yb.install(**kwarg)
yb.resolveDeps()
yb.buildTransaction()
yb.processTransaction()

You need to first check if the pacakge is installed, if so, then skip it:
yb.conf.cache = 1 # enable cache
installed = yb.rpmdb.returnPackages()
packages = ['a','b','c']
for package in packages:
if package in installed:
print('{0} is already installed, skipping...'.format(package))
else:
print('Installing {0}'.format(package))
yb.install(package)
yb.resolveDeps()
yb.buildTransaction()
yb.processTransaction()

Related

Using packages in PyWeka(Python Weka Wrapper)

I need some help with using installed packages in PyWeka. I am able to install packages, but I am unable to use them or find where they are installed. When I try to find the full classname, i get an exception(which occurs when there is no matching module)
Example:
import weka.core.classes as core
core.complete_classname("J48")
Output
'weka.classifiers.trees.J48'
I am trying to install the DMNBtext package. Installation occurs but module cannot be found
import weka.core.classes as core
print(packages.is_installed("DMNBtext"))
core.complete_classname("DMNBtext")
Output
True
---------------------------------------------------------------------------
Exception Traceback (most recent call last)
<ipython-input-25-7ea05097d6f1> in <module>()
1 import weka.core.classes as core
2 print(packages.is_installed("DMNBtext"))
----> 3 core.complete_classname("DMNBtext")
/usr/local/lib/python3.6/dist-packages/weka/core/classes.py in complete_classname(classname)
1725 return str(result[0])
1726 elif len(result) == 0:
-> 1727 raise Exception("No classname matches found for: " + classname)
1728 else:
1729 matches = []
Exception: No classname matches found for: DMNBtext
Please note that is_installed gives True output, meaning the package is installed.
Any idea how I can resolve this ?
Also,my jvm was started with packages=True, so that should not be a problem.
Thanks in advance.
I just created a new virtual environment with python-weka-wrapper3:
virtualenv -p /usr/bin/python3.6 pww3
./pww3/bin/pip install numpy matplotlib pygraphviz javabridge python-weka-wrapper3
And then ran the following script successfully (needs to be run twice, if the DMNBtext package is not yet installed):
import sys
import weka.core.jvm as jvm
import weka.core.packages as packages
from weka.core.classes import complete_classname
jvm.start(packages=True)
pkg = "DMNBtext"
# install package if necessary
if not packages.is_installed(pkg):
print("Installing %s..." % pkg)
packages.install_package(pkg)
print("Installed %s, please re-run script!" % pkg)
jvm.stop()
sys.exit(0)
# testing classname completion
print(complete_classname(".J48"))
print(complete_classname(".DMNBtext"))
jvm.stop()
Once the DMNBtext package is installed, the script outputs this:
weka.classifiers.trees.J48
weka.classifiers.bayes.DMNBtext
I am going to assume you already have the pyWeka installed and running on Anaconda. The details of how to do this are here.
You should start activating the java virtual machine
import weka.core.jvm as jvm
import weka.core.classes as core
jvm.start(packages=True) # needed for package manipulation
from weka.classifiers import Classifier # you are going to classify something
import weka.core.packages as packages # so you can install packages
packages.install_package('DNMNBtext') # if it is not already installed.
# You can also install it on Weka
And now you can issue:
core.complete_classname("DMNBtext")
to find the name of the class
'weka.classifiers.bayes.DMNBtext'
Finally
dmnb = Classifier(classname="weka.classifiers.bayes.DMNBtext")
dmnb.options=[list of options]
Take care

Cannot install custom python module

I am having trouble installing a custom python module I have written.
Here are my steps so far:
Navigate to the module directory C:\Users\myname\repos\mymodulename where the setup.py file is in the anaconda prompt. Type: python setup.py install
The command prompt returns (plus some other things)
Extracting mymodulename-0.1-py3.7.egg to c:\users\myname\appdata\local\continuum\anaconda3\lib\site-packages
mymodulename 0.1 is already the active version in easy-install.pth
Installing myclass-script.py script to C:\Users\myname\AppData\Local\Continuum\anaconda3\Scripts
Installing myclass.exe script to C:\Users\myname\AppData\Local\Continuum\anaconda3\Scripts
Installed c:\users\myname\appdata\local\continuum\anaconda3\lib\site-packages\mymodulename-0.1-py3.7.egg
Processing dependencies for mymodulename==0.1
...
Using c:\users\myname\appdata\local\continuum\anaconda3\lib\site-packages
Finished processing dependencies for mymodulename==0.1
To me that looks like it has installed. Opening up the console and trying to import:
>>> import mymodulename.myclassas ce
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'mymodulename'
>>>
It appears in hasn't. Checking the list of modules in anaconda with help('modules') confirms that it has not been imported.
I thought that perhaps i had installed it to the wrong environment:
(base) C:\Users\myname>conda env list
# conda environments:
#
base * C:\Users\myname\AppData\Local\Continuum\anaconda3
py2 C:\Users\myname\AppData\Local\Continuum\anaconda3\envs\py2
Only a python 2 environment which mymodule wouldn't be compatible with.
Does anyone have any suggestions about what I can try to resolve this? Happy to elaborate on any of the points
Thanks in advance.
EDIT: Some more information that may be relevant.
This package was initially installed in site-packages. I have reinstalled there and the package works. The reason I moved the package is because I am aware it is bad practice to store custom packages there.

Error importing tag from pattern3.en

I have installed pattern3 with pip in a conda virtual environment. I am working in Windows 8.1 64-bit .When I try to execute
from pattern3.en import tag
I have the following errror:
Traceback (most recent call last):
File "test_pattern.py", line 5, in <module>
from pattern3.en import tag
File "C:\Users\Rodolfo\Anaconda3\envs\flasky\lib\site-packages\pattern3\text\en\__init__.py", line 22, in <module>
from pattern3.text import (
File "C:\Users\Rodolfo\Anaconda3\envs\flasky\lib\site-packages\pattern3\text\__init__.py", line 28, in <module>
from pattern3.text.tree import Tree, Text, Sentence, Slice, Chunk, PNPChunk, Chink, Word, table
File "C:\Users\Rodolfo\Anaconda3\envs\flasky\lib\site-packages\pattern3\text\tree.py", line 37
except:
^
IndentationError: expected an indented block
Any idea or help will be appreciated :)
I solved the issue.
I received a message from Markus Beuckelmann (github) : "...you are using the deprecated pattern3 repository which contains a completely different code base that is not maintained anymore. There is a development branch here on clips/pattern with Python 3 support. You can clone it, git clone -b development https://github.com/clips/pattern and install with pip or conda....."
However, I had many problems after cloning the repository and running python setup.py install
Finally, I was able to install pattern using:
pip install git+https://github.com/your_user_id/pattern.git#development
Where your_user_id is your github user id which is inserted automatically when the repository path is copied to the clipboard.
None of the previous answers solved my problem. At least not for the pattern3 module which is available on PyPI through pip.
I solved this by entering to the problematic file (C:\Python27\Lib\site-packages\pattern3\text\tree.py) and fixing the problem myself:
from itertools import chain # 34
try: # 35
None # ===> THIS IS THE LINE I ADDED! <===
except: # 37
izip = zip # Python 3
Obviously, this is not a suitable answer for production environments.
Alternatively, clone the current pattern repository on your computer and install it:
git clone -b development https://github.com/clips/pattern
cd pattern
python setup.py install
Then you must import pattern instead of pattern3
In case anyone still have issue, you can run the command
sed -i '36s/.*/ pass/' /usr/local/lib/python3.6/dist-packages/pattern3/text/tree.py
I was using a dockerfile for the project so did the same.

Unable to import git in python

I am facing these issues. Can you help me with the same ?
Why am I seeing this error ? Do I have to add anything in the requirements.txt file ?
>>> import git
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
import git
File "git\__init__.py", line 29, in <module>
_init_externals()
File "git\__init__.py", line 23, in _init_externals
raise ImportError("'gitdb' could not be found in your PYTHONPATH")
ImportError: 'gitdb' could not be found in your PYTHONPATH
>>> from git import Repo
Traceback (most recent call last):
File "<pyshell#8>", line 1, in <module>
from git import Repo
File "git\__init__.py", line 29, in <module>
_init_externals()
File "git\__init__.py", line 23, in _init_externals
raise ImportError("'gitdb' could not be found in your PYTHONPATH")
ImportError: 'gitdb' could not be found in your PYTHONPATH
I already had gitdb and smmap installed so I had to reinstall them.
You can reinstall them by running the following command in your terminal:
pip3 install --upgrade --force-reinstall gitdb; pip3 install --upgrade --force-reinstall smmap
I also got the message ImportError: 'gitdb' could not be found in your PYTHONPATH (when trying to use GitPython).BUT I had gitdb already installed!
Thanks to this hint I figured out that gitdb silently failed because it was missing smmap.
So I installed this and it worked.
You need to install gitdb package.
$ sudo easy_install gitdb
I had the same problem. However, gitdb and smmap were already installed by pip. As I used brew to install python and its dependencies on my mac, when I checked brew doctor command, it said that my /usr/local/sbin directory is not in my PATH. So I added it to my PATH (though it didn't have anything to do with the python) and everything worked out eventually.
MS Windows Versions of this problem can occur because of the order of Python versions in your system PATH, as it did for me. I did not realize that when I installed another program, it installed a newer version of Python for its own usage, and it appended my system PATH with the address to the newer version. I noticed it when I looked at the PATH variable and found two versions of Python being called. Windows uses the first it finds, and if the first doesn't match what your program expects, it gets confused and can't find the right path to the module. This is what I did to resolve it:
To check: an easy way to test if this is your problem is to see if the paths separated by semicolons are in the right order. That can be seen in the System Variables of Windows or by printing your PATH variable in your CMD shell like in this example:
C:> path
PATH=C:\Program Files (x86)\Python37-32\Scripts;C:\Program Files (x86)\Python37-32;C:\Program Files\Python38\Scripts;C:\WINDOWS
Temporary solution:
To see if it is going to fix your computer, change it in your CMD window. Your variable change will be discarded when the window is closed. One way to do this test is to copy the paths, move the Python references to the order they are needed, and write it back:
C:> set path = C:\WINDOWS;C:\Program Files (x86)\Python37-32;C:\Program Files\Python38\Scripts;C:\Program Files (x86)\Python37-32\Scripts\
Then run the Python program to see if this was your problem. Note that this is only an example; do not copy & paste it. Your path is customized for the programs on your computer.
Permanent solution: If the above test resolves your problem, you must change your System Variables to make the change permanent. For me that usually requires a reboot afterwards in order to make the variables appear in all new windows.

error while trying to install a package using the python-apt API

I found a code which i need. It is from this link : How to install a package using the python-apt API
#!/usr/bin/env python
# aptinstall.py
import apt
import sys
pkg_name = "libjs-yui-doc"
cache = apt.cache.Cache()
cache.update() # error is in this line
pkg = cache[pkg_name]
if pkg.is_installed:
print "{pkg_name} already installed".format(pkg_name=pkg_name)
else:
pkg.mark_install()
try:
cache.commit()
except Exception, arg:
print >> sys.stderr, "Sorry, package installation failed [{err}]".format(err=str(arg))
However i can't make it work. I searched about the problem on the web. It is said that there should be no package manager,apt,pip etc active in order to work with this code. However, no package manager,apt,pip etc. is open in my computer. I thought that when computer starts, some package manager can be active. So i typed
ps -aux
in terminal and look at the active processes, but i didn't see any active process related to package manager(i'm not %100 sure about this, because any process i don't know can be related to package manager.But how could i know it?) To sum up,i started the computer and opened only terminal. Then i typed python aptinstall.py and hit enter. I take the following error :
Traceback (most recent call last):
File "aptinstall.py", line 7, in <module>
cache.update()
File "/usr/lib/python2.7/dist-packages/apt/cache.py", line 397, in update
raise LockFailedException("Failed to lock %s" % lockfile)
apt.cache.LockFailedException: Failed to lock /var/lib/apt/lists/lock
I delete the lock by giving the command in terminal :
sudo rm /var/lib/dpkg/lock
It didn't work too.
How can i solve this problem? Any idea will be appreciated.
Please try looking for update-manager in ps. It runs automatically on a periodic basis, so it may be locking the apt db.
There are three different reasons which cause this error.
1 - As i mentioned earlier, if any package manager is runnning(for example;pip,apt-get,synaptic,etc), it gives the error.
2 - If you are using your ubuntu in a virtual machine, this causes the same error.
3 - If you are running your program without root privileges, this causes the same error. For example ,if you are running your program with "python aptinstall.py" you get the error, running the program with "sudo python aptinstall.py" is the correct one.

Categories

Resources