5

I used pip to install two libraries I need, which are related in what they do but cannot dispose any of them. The problem is that once unpacked, they have the same name and the functionalities in both of them are imported as:

import the_package
from the_package import a, b

Update: I mean to import the_package in any of the previous ways, not necessarily both nor in sequence.

Since I install them via pip, and are installed from a requirements file so my teammates can install them the same way I guess renaming packages/modules is not an option (if it is, I appreciate pointing how to do it automatically)

One thing I came up with was giving pip some option that would install the packages in directories with some sort of alias/prefix so their names could be different, but pip docs didn't come much in handy for me.

Thanks in advance for any help :D

Gerard
  • 9,088
  • 8
  • 37
  • 52

2 Answers2

4

You should add this while installing

pip install --install-option="--prefix=$PREFIX_PATH" package_name

and install the two packages to different folders. Then import them as

import Folder1.mymodule as A
import Folder2.mymodule as B

Might also want to inform the package creators.

Pratik Mandrekar
  • 9,362
  • 4
  • 45
  • 65
  • Sorry, this didn't work for me. Here is my output: https://gist.github.com/3289595 – Gerard Aug 07 '12 at 21:42
  • 1
    I believe @Pratik was simply using `$PREFIX_PATH` as a placeholder. You need to either define that environment variable or hardcode the prefix you want. – Chris Pratt Aug 07 '12 at 21:52
  • Yes @ChrisPratt is right. Declare your own path variable for each of the folder locations. Also the gist you pasted indicates a privilege issue. Use sudo or provide a non system path in your user home. – Pratik Mandrekar Aug 08 '12 at 05:05
  • This is helpful but it's only part of the solution. Also see: https://stackoverflow.com/questions/32884206/python-importing-different-module-with-same-name – dmn Jul 31 '17 at 14:53
-1
import the_package
from the_package import a, b

and then:

import the_package as package_b
from the_package import a as a_, b as b_

use them accordingly and this will not conflict with the namespaces.

James R
  • 4,571
  • 3
  • 30
  • 45