Search code examples
pythoncdistutilssetup.pypypi

access the user's activated virtual environment from within setup.py when they pip install your package from PyPI


My package Foo builds an extension libFoo.so that depends on another package Bar's extension libBar.so, and I don't know a priori where the dependency libBar.so is already installed. I need to know the dependency's directory in order to set libFoo.so's rpath so that it can find libBar.so at runtime.

I thought a good way to do this might be to import the dependency package Bar in my setup.py and inspect the module to get the directory, since I know where libBar.so will be relative to the Bar package's directory. That way if they installed from a virtual environment, I would locate the appropriate path.

However, when I try to do this, i.e., when I activate a virtual environment and install my package from PyPI, I find that import Bar in the setup.py ends up importing a version from a DIFFERENT virtual environment (I can tell by inspecting the built libFoo.so's rpath after installation).

On the other hand, if I install the Foo package locally by navigating to the source directory and doing pip install -e . with my virtual environment activated, the rpath is set correctly...

How can I get the right behavior when installing from PyPI? In case it helps, here is the relevant code from setup.py:

import Bar
barDir = os.path.dirname(inspect.getfile(Bar))

When I activate my environment and install from PyPI, barDir is

/Users/me/anaconda/envs/WRONGENVIRONMENT/lib/python2.7/site-packages/bar

When I activate my environment and install locally, barDir is

/Users/me/anaconda/envs/RIGHTENVIRONMENT/lib/python2.7/site-packages/bar

I'm running OS X 10.10.5 and using conda environments


Solution

  • It turned out to be a stupid caching problem (pip install was not getting the latest package from PyPI, but using a cached copy of an older version I had been testing with). I had to do

    pip install --no-cache-dir Foo
    

    to ensure I was getting a fresh version