Two libraries are found on Pypi, 'python-magic' and 'filemagic', which can be imported with same import statement:
https://pypi.org/project/python-magic/
https://pypi.org/project/filemagic/
import magic
I want to import both of these libraries in my django project?
I want to consume magic.from_buffer() method from python-magic lib & magic.id_buffer() method from libmagic.
I have pip installed both python-magic, libmagic, filemagic into my project. Also since I need to upload this project in custom server setup, making changes in to these packages in my venv won't help.
I tried importing magic using AS in import statements
from filemagic import magic as mm (code does not identify filemagic in this statement) from python-magic import magic as pm (code does not identify python-magic in this statement)
EDIT - For a clearer objective, I want to identify file type on content basis. I have two different types of file inputs, 1. user uploaded file, 2. user uploaded file in base64 encoded format.
I have already been using magic.Magic().id_buffer to read files of type 1.
I now want to add code to handle base64 encoded files as solved in this post How to know MIME-type of a file from base64 encoded data in python?
when run on server(other than my local dev env), I get 'magic' module has no attribute 'from_buffer' error.
This is possible only with some big tradeoffs. Python
imports of a package roughly looks like this:sys.pathmagic)In the process of installing a package, all files of the package that gets installed get stored in the virtualenv in a subdirectory named after how it should be imported (In your case
magic).This creates a big problem when having two packages with the same name under which they store their files. The two packages will both write their files into the same directory.
In your case it's pretty lucky, only the
__init__.pyfile gets overwritten by the package that gets installed second. This means that you can still use any contents provided by both packages.But now instead of
import magicorfrom magic import ...you need to useimport magic.<modulename>orfrom magic.<modulename> import ....