简体   繁体   English

当他们从PyPI pip安装软件包时,从setup.py中访问用户激活的虚拟环境

[英]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. 我的软件包Foo构建了一个扩展libFoo.so,它依赖于另一个软件包Bar的扩展libBar.so,而我不知道先验的依赖库libBar.so已被安装。 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. 我需要知道依赖项的目录以便设置libFoo.so的rpath,以便它可以在运行时找到libBar.so。

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. 我认为这样做的一个好方法可能是在我的setup.py中导入依赖包Bar并检查模块以获取目录,因为我知道libBar.so相对于Bar包目录的位置。 That way if they installed from a virtual environment, I would locate the appropriate path. 这样,如果它们是从虚拟环境安装的,我将找到合适的路径。

However, when I try to do this, ie, 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). 但是,当我尝试执行此操作时(即,当我激活虚拟环境并从PyPI安装我的软件包时),我发现setup.py中的import Bar最终从不同的虚拟环境中导入了一个版本(我可以通过检查安装后构建的libFoo.so的rpath)。

On the other hand, if I install the Foo package locally by navigating to the source directory and doing pip install -e . 另一方面,如果我通过导航到源目录并执行pip install -e .在本地安装Foo软件​​包pip install -e . with my virtual environment activated, the rpath is set correctly... 激活我的虚拟环境后,rpath设置正确...

How can I get the right behavior when installing from PyPI? 从PyPI安装时如何获得正确的行为? In case it helps, here is the relevant code from setup.py: 如果有帮助,请参阅setup.py中的相关代码:

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

When I activate my environment and install from PyPI, barDir is 当我激活环境并从PyPI安装时,barDir是

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

When I activate my environment and install locally, barDir is 当我激活环境并在本地安装时,barDir是

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

I'm running OS X 10.10.5 and using conda environments 我正在运行OS X 10.10.5并使用conda环境

Try using pkg_resources from setuptools : https://pythonhosted.org/setuptools/pkg_resources.html . 尝试从setuptools使用pkg_resourceshttps : setuptools

Something like: 就像是:

from pkg_resources import resource_string
libbar = resource_string('Bar', 'libBar.so')

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). 事实证明这是一个愚蠢的缓存问题(pip安装没有从PyPI获取最新的软件包,而是使用了我测试过的较早版本的缓存副本)。 I had to do 我必须做

pip install --no-cache-dir Foo

to ensure I was getting a fresh version 确保我得到的是新版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM