__init__() got an unexpected keyword argument 'mime' in python/django - python

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.

Related

Can't import module after manually installing

I have installed the module pynauty via https://web.cs.dal.ca/~peter/software/pynauty/html/install.html. Everything seemed to work, and I believe that it is fully installed. However, when I try importing it in python shell, it says there is no module named pynauty. I think I am missing a final step in installing. Does anyone know what that final step would be?
I just grappled with this problem myself. As creimers pointed out, the setup script calls pip install --user . which installs pynauty in ~/.local/lib/pythonX.X/site-packages.
What you probably want is to have it installed in /usr/bin/python (or whatever pops up when you type which python). Assuming you already called make user-ins, all you need to do is run
sudo pip install .
from inside where you extracted and built your pynauty.

editing code of a python module

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

How to install a package using the python-apt API

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.

biopython test_Tutorial ... FAIL

I'm fairly new to programming. I'm trying to install biopython on mac os x 10.5.
This is what I did so far. 1. installed xcode 2. installed numpy 3. ran these commands in terminal python setup.py build python setup.py test
test reported back one fail.
test_Tutorial ... FAIL
ERROR: Run tutorial doctests.
Traceback (most recent call last): File "test_Tutorial.py", line 152, in test_doctests ValueError: 4 Tutorial doctests failed: test_from_line_05671, test_from_line_06030, test_from_line_06190, test_from_line_06479
Thanks for any help or advice.
That file test_Tutorial.py runs marked examples in the source for the main Biopython Tutorial and Cookbook ( http://biopython.org/DIST/docs/tutorial/Tutorial.html / http://biopython.org/DIST/docs/tutorial/Tutorial.pdf ) to check the examples work as expected. Internally this uses the same library as Python's doctest examples.
The fact that test_Tutorial.py failed is probably a harmless issue with a couple of examples.
Which version of Biopython are you using? If this was an official release that failure is unexpected. If it was a snapshot from the git repository then that is unfortunate. If you are curious, you could try this to see more information:
$ cd Tests
$ python test_Tutorial.py
This kind of issue might be more easily discussed on the Biopython mailing list http://biopython.org/wiki/Mailing_lists
I've found that the easiest way to set up a good programming environment is to use MacPorts, as it has a great way of ensuring that all dependencies are satisfied when installing things. You'll need to be familiar with Terminal and the command line, though.
Install MacPorts for OSX 10.5. Read the documentation too.
Restart.
Open Terminal.app and enter sudo port selfupdate to make sure the portfile definitions are up to date.
Run sudo port install py27-biopython to install the latest version of Python 2 (2.7.3), numpy, and biopython. This will take a while.
Run echo $PATH and make sure that /opt/local/bin and /opt/local/sbin` are at the beginning. They should be.
Run which python and ensure it returns /opt/local/bin/python. If it doesn't, run sudo port install python_select and follow its instructions to select your default python version.
Hopefully, at this point you can run python to enter the interactive interpreter, and import Bio won't give any errors.
Good luck!

Error starting Django with Pinax

Upon trying to start a Pinax app, I receive the following error:
Error: No module named notification
Below are the steps I took
svn co http://svn.pinaxproject.com/pinax/trunk/ pinax
cd pinax/pinax/projects/basic_project
./manage.py syncdb
Any suggestions?
UPDATE:
Turns out there are some bugs in the SVN version. Downloading the latest release solved my problem. If any one has any other suggestions on getting the trunk working, they would still be appreciated.
I'd avoid the svn version all together. It's unmaintained and out of date. Instead, use the git version at http://github.com/pinax/pinax or (even better) the recently release 0.7b3 downloadable from http://pinaxproject.com
Two thoughts:
1. Check all of your imports to make sure that notification is getting into the namespace.
2. You may be missing quotes around an import path (eg. in your urls.py: (r'^test', 'mysite.notification') -- sometimes I forget the quotes around the view)
Try following the latest install instructions here:
http://github.com/pinax/pinax/blob/600d6c5ca0b45814bdc73b1264d28bb66c661ac8/INSTALL
Don't think this will work on Windows (maybe if you are using cygwin) as they are using virtualenv and pip.
Note the version has recently been upgraded to 0.7rc1
IIRC I had to add a directory or two to the Python path last time I did a fresh install of Pinax. I'm doing a fresh checkout now into a new virtualenv, I'll edit this answer if I hit any snags.

Categories