How to import two libraries with the same name

Viewed 508
2 Answers

So I don't know if you are using a virtual environment, but the process is similar for the case in which you are, and the case in which you installed both modules globally. Basically, what you are doing with an import statement is saying "import this file" or "import this package". Now a package is simply a folder with multiple files in it. So you just have to go to the directory in which you have installed the packages and look if the file names (or the folder names) are exactly the same. Now if they are, you have to rename one of them to something else. If they aren't, instead of using the standard import statement with the "magic" in it, you can give it the path to the package/file. So it would be "import C:\path\to\your\installed\package" (this is for windows, I assume you use windows). Now you have to use two backslashes, because if you were to use one, you would be escaping the next character, whereas with two you are telling him that you actually want one backslash. I think this should fix the issue, let me know.

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.

Related