Search code examples
pythonpython-2.7pippython-importeasy-install

Distinguishing between Python modules of the same name / installing with different name?


Two separate Python wrappers have been made for the Firebase REST API:

https://github.com/mikexstudios/python-firebase

https://pypi.python.org/pypi/python-firebase/1.2

Both have their strengths and drawbacks, so now I want to use one for some API actions, and the other for other API actions, in the same program. The problem is, when installed, they are both known as firebase.

Is it possible to pip install one or both with a different name? If not, does the import statement have the intelligence to distinguish, if used correctly?


Solution

  • When importing a module, python searches the paths in sys.path by order and stops at first match. So a simple import firebase will not work.

    There is a brittle solution to choose one or the other, but you will not be able to import both.

    Anyway, to choose one or the other, you can simply import an internal name of the packages. If we look at the two packages' exposed names, we get:

    https://github.com/mikexstudios/python-firebase
     firebase/
      __init__.py
       Firebase
       requests
       urlparse
       os
       json
    
    https://github.com/ozgur/python-firebase
     firebase/
      __init__.py
       atexit
       process_pool
       close_process_pool
       urlparse
       json
       FirebaseTokenGenerator
       http_connection
       process_pool
       JSONEncoder
       ...
    

    So, you can either choose the first one by importing a name only present in it:

    from firebase import requests
    

    Or the second, with the same reasoning:

    from firebase import atext
    

    But frankly, this is horrible IMO.