I'm quite a newbie when it comes to Python, thus I beg foregiveness beforehand :). That said, I'm trying to make a script that, among other things, installs some Linux packages. First I tried to use subopen as explained here. While this can eventually work, I stumbled upon the python-apt API and since I'm not a big fan or re-inventing the wheel, I decided to give a try.
Problem comes when trying to find examples/tutorials on installing a package using python-apt. Searching the documentation I found the PackageManager class that has some methods to install a package. I tried some simple code to get this working:
apt_pkg.PackageManager.install("python")
This does not seem to work that easily, the install method expects apt_pkg.PackageManager instead of a plain String. Thus, looking a bit more, I found this example that looks promising, but I'm a bit reluctant to use since I don't really understand some of what is happening there.
Then, has anyone tried to install a package using python-apt or should I go for using plain-old subopen style?
Thanks!
It's recommended to use the apt module from the python-apt Debian package. This is a higher level wrapper around the underlying C/C++ libapt-xxx libraries and has a Pythonic interface.
Here's an example script which will install the libjs-yui-doc package:
#!/usr/bin/env python
# aptinstall.py
import apt
import sys
pkg_name = "libjs-yui-doc"
cache = apt.cache.Cache()
cache.update()
cache.open()
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))
As with the use of apt-get, this must be run with superuser privileges to access and modify the APT cache.
$ sudo ./aptinstall.py
If you're attempting a package install as part of a larger script, it's probably a good idea to only raise to root privileges for the minimal time required.
You can find a small example in the /usr/share/pyshared/apt/progress/gtk2.py:_test() function showing how to install a package using a GTK front-end.
Related
Recently whenever I installed a new enviroment, I got below message a lot. Question is after installing this, How do I use this new package? For my existing file, do I need to change all relevant import packages statements?
Windows 64-bit packages of scikit-learn can be accelerated using scikit-learn-intelex.
More details are available here: https://intel.github.io/scikit-learn-intelex
For example:
$ conda install scikit-learn-intelex
$ python -m sklearnex my_application.py
I found a webpage about this:
https://pypi.org/project/scikit-learn-intelex/
look like everything is the same( like import functions, packages) except adding from sklearnex import patch_sklearn? Is it correct?
Correct,basically the way this would work - for some of sklern calls optimized version would be used instead of stock scikit-learn.
There are also other ways how you can patch https://intel.github.io/scikit-learn-intelex/what-is-patching.html#term-patching
For example you can do global patching that would not require changes in python scripts/notebooks
python sklearnex.glob patch_sklearn
I have installed a pip package, but trying to use it I get errors about some modules or classes not being available. How can I investigate the root cause myself?
Motivation
On OpenSuse Leap 15.1, no binary package for AutoKey is available. As a consequence I was trying to install it with
pip install --user autokey # --user because I'm not root at work.
After executing this, when I try to run the GUIs, I get import-related exceptions:
For autokey-gtk: ValueError: Namespace AppIndicator3 not available, raised by gi.require_version(...). [1]
For qutokey-qt: ImportError: cannot import name 'Qsci', raised by an from ... import Qsci line.
While trying to figure out how to resolve the error, likely related to missing dependencies, I started to wonder: Is there any way to figure out what is missing from the error message? Running pip3 search Qsci and pip3 search AppIndicator3 would seem an obvious solution, but don't yield any results.
I intentionally omit the full backtrace for now in order to avoid distracting from the core question: How can I try to find the solution myself?
Documentation of package you've installed should have instructions about any external dependencies on various platforms.
Second option is to use pip_missing_reqs.
I recognize this error as specific to the way PyGObject handles dependencies. You should see a line similar to the following in the stack trace:
File "/path/to/blah/indicator.py", line 31, in <module>
gi.require_version('AppIndicator3', '0.1')
Once you install the appropriate GLib/Gtk feature, PyGObject will be able to find it via GObject introspection, and automatically bind to it, so there's no Python-specific package that needs to be installed to access the app indicator feature.
On my Ubuntu 18.04 system, installing these packages was enough:
$ sudo apt install gir1.2-appindicator3-0.1 libappindicator3-0.1-cil
Here are details about the two packages:
https://packages.ubuntu.com/bionic/gir1.2-appindicator3-0.1
https://packages.ubuntu.com/bionic/libappindicator3-0.1-cil
I'm not convinced that the second one was necessary.
Apologies if this is a very stupid question but I am new to python and although I have done some googling I cannot think how to phrase my search query.
I am writing a python script that relies on some libraries (pandas, numpy and others). At some point in the future I will be passing this script onto my University so they can mark it etc. I am fairly confident that the lecturer will have python installed on their PC but I cannot be sure they will have the relevant libraries.
I have included a comments section at the top of the script outlining the install instructions for each library but is there a better way of doing this so I can be sure the script will work regardless of what libraries they have?
An example of my script header
############### - Instructions on how to import libraries - ###############
#using pip install openpyxl using the command - pip install openpyxl
#########################################################################
import openpyxl
import random
import datetime
Distributing code is a huge chapter where you can invest enormous amounts of time in order to get things right, according to the current best practices and what not. I think there is different degrees of rightness to solutions to your problem, with more rightness meaning more work. So you have to pick the degree you are comfortable with and are good to go.
The best route
Python supports packaging, and the safest way to distribute code is to package it. This allows you to specify requirements in a way that installing your code will automatically install all dependencies as well.
You can use existing cookiecutters, which are project-templates, to create the base you need to build packages:
pip install cookiecutter
cookiecutter https://github.com/audreyr/cookiecutter-pypackage
Running this, and answering the ensuing questions, will leave you with python code that can be packaged. You can add the packages you need to the setup.py file:
requirements = ['openpyxl']
Then you add your script under the source directory and build the package with:
pip wheel .
Let's say you called your project my_script, you got yourself a fresh my_script-0.1.0-py2.py3-none-any.wheel file that you can send to your lecturer. When they install it with pip, openpyxl will be automatically installed in case it isn't already.
Unfortunately, if they should also be able to execute your code you are not done yet. You need to add a __main__.py file to the my_script folder before packaging it, in which you import and execute the parts of your code that are runnable:
my_script/my_script/__main__.py:
from . import runnable_script
if __name__ == '__main__':
runnable_script.run()
The installed package can then be run as a module with python -m my_script
The next best route
If you really only have a single file and want to communicate to your lecturer which requirements are needed to run the script, send them both your script and a file called requirements.txt, which contains the following lines:
openpyxl
.. and that's it. If there are other requirements, put them on separate lines. If the lecturer has spent any amount of time working with python, they should know that running pip install -r requirements.txt will install the requirements needed to run the code you have submitted.
The if-you-really-have-to route
If all your lecturer knows how to do is entering python and then the name of your script, use DudeCoders approach. But be aware that silently installing requirements without even interactive prompts to the user is a huge no-no in the software-engineering world. If you plan to work in programming you should start with good practices rather sooner than later.
You can firstly make sure that the respective library is installed or not by using try | except, like so:
try:
import numpy
except ImportError:
print('Numpy is not installed, install now to continue')
exit()
Now, if numpy is installed in his computer, then system will just import numpy and will move on, but if Numpy is not installed, then the system will exit python logging the information required, i.e., x is not installed.
And implement the exact same for each and every library you are using.
But if you want to directly install the library which is not installed, you can use this:
Note: Installing libraries silently is not a recommended way.
import os
try:
import numpy
except ImportError:
print('Numpy is not installed, installing now......')
resultCode = os.system('pip install numpy')
if resultCode == 0:
print('Numpy installed!')
import numpy
else:
print('Error occured while installing numpy')
exit()
Here, if numpy is already installed, then the system will simply move on after installing that, but if that is not installed, then the system will firstly install that and then will import that.
Running pymatlab on my machine results in
Exception AttributeError: "'MatlabSession' object has no attribute 'engine'" in > ignored
after the command session = pymatlab.session_factory() is run.
How to fix this problem has been discussed already here:
Running MATLAB from Python
It looks like one line of code in the sessionfactory.py script in the pymatlab module has to be changed in a minor way. The problem I have is that the pymatlab module which is installed on my machine is in .egg form and it doesn't look like it is possible to change the code directly with a text editor. Any suggestions on how to do that?
Thanks
If you use easy_install, check
How do I forbid easy_install from zipping eggs?
If you prefer pip (and you probably should), check
pip: “Editable” Installs, i.e.
pip install -e pymatlab
The first line of this code in .py file returns an error.
mime = magic.Magic(mime=True)
content_type = mime.from_buffer((data).read(1024))
request.session['content_type'] = content_type
if content_type == 'application/pdf' or content_type == 'application/msword':
request.session['upload_status'] = "Content type is valid according to (MAGIC)"
The error message is
__init__() got an unexpected keyword argument 'mime'
I'm using Django 1.4.1 and Python 2.7.3. I have Magic installed. No clue whats going wrong- any help is appreciated!
Just a stab in the dark but The documentation would suggest that you shouldn't be instantiating the Magic class directly.
import magic
magic.from_buffer(open("testdata/test.pdf").read(1024))
The Magic object's constructor does not accept an argument named 'mime'. I would suggest looking at the doc string with help(magic.Magic); it may give you a clue.
As per suggestions above, if connection to JIRA server fails with error:
The error message is __init__() got an unexpected keyword argument 'mime'
Then go edit
/usr/lib/python2.7/site-packages/jira/client.py
Replace
self._magic = magic.Magic(mime=True)
with
self._magic = magic
Then run code like this:
from jira.client import JIRA
import magic
...
jira = JIRA(options={'server':'https://jira.server.goes.here'}, basic_auth=(options.username, options.password))
I'm using python 2.7.3 with jira-python (http://jira-python.readthedocs.org/en/latest/)
You most likely have another version of magic. If I remember correctly from the last time I looked this stuff up, there's a different version that comes with Ubuntu (maybe, there's also something called filemagic). The same thing is the case on the out-of-the-box version of Cygwin. That was my case.
I had reinstalled Cygwin and ran into this same issue - two versions of magic / python-magic / filemagic for Python. I looked back here on SO for the solution, but didn't find it quickly. Luckily, I kept notes from before, and the solution that worked for me was:
$ sudo pip3 uninstall filemagic
$ sudo pip3 install python-magic
OR, what worked better on my Cygwin installation:
$ python -m pip uninstall filemagic
$ python -m pip install python-magic
That fixed the problem for me.
As I searched for more on this problem, I came across a very similar solution here (archived), on github. There is an additional step.
uninstall filemagic:
sudo pip3 uninstall filemagic
uninstall python-magic:
sudo pip3 uninstall python-magic
(Possible addition)
sudo apt-get uninstall python-magic
install python-magic:
sudo pip3 install python-magic
With a quick search, I couldn't find details of the two versions of magic. I just found some comments on threads saying, "You must have the other version of magic", or "You must have a different version of magic".
Edit
I've updated this answer with the magic sources. From this, it seems that you might have to do another command before installing python-magic with pip
sudo apt-get uninstall python-magic
as noted above.
Edit
I found the details about the different versions of magic that are floating around.
I have the following three links ( first , second , third )
Basically, there are two versions going around. The one which has the most support is on PYPI and (it seems to me) is used much more often. It is v.0.4.15 currently (2020-02-19), and its github page is the first link. At that page, you can read the following:
Name Conflict
There are, sadly, two libraries which use the module name magic. Both have been around for quite a while. If you are using this module and get an error using a method like open, your code is expecting the other one. Hopefully one day these will be reconciled.
The second version, which seems to cause the most problems, is currently (2020-02-19) v5.25. According to the third link and my own research, this one gets installed when one uses sudo apt-get install python-magic on certain versions of Ubuntu. (Look here for some possible details from Ubuntu.)
The best explanation of all this is found in the second link. Props to #mhawke for explaining things so well.
Here are archived versions of the links above: archived first, archived second, archived third, archived Ubuntu information.