sinavix742
sinavix742

Reputation: 90

How to import two libraries with the same name

In Python there are two libraries that can be found on Pypi 'python-magic' and 'filemagic':

As can be seen from the documents, to import either one, the import statement is simply:

import magic

So how would you import both of these libraries in a project?

Upvotes: 4

Views: 994

Answers (1)

jgmh
jgmh

Reputation: 689

In order to use both of the libraries, you need to edit the packages. For instance, I installed inside a virtualenv python-magic. This created a directory magic inside site-packages. Changing that name to pymagic and also changing every occurrence of magic to pymagic in the files loader.py, compat.py and __init.py__ seems to solve the problem. After that you can install filemagic and you can import the libraries as:

import pymagic #python-magic
import magic #filemagic

This solution is particular to these two packages.

Upvotes: 1

Related Questions