python pip指定一个库目录和一个包含目录

我正在使用pip,并尝试安装一个名为pyodbc的python模块,这个模块对unixodbc-dev,unixodbc-bin,unixodbc等非python库具有一定的依赖性。 目前我无法在系统范围内安装这些依赖项,因为我只是在玩,所以我把它们安装在一个非标准的位置。 如何告诉pip在哪里寻找这些依赖关系? 更确切地说,如何在构buildpyodbc扩展时使用包含dirs(gcc -I)和库dirs(gcc -L -l)的pip来传递信息?

pip有一个--global-option标志

您可以使用它将附加标志传递给build_ext
例如,要添加一个–library-dirs(-L)标志:
pip install --global-option=build_ext --global-option="-L/path/to/local" pyodbc

gcc也支持环境variables: http : //gcc.gnu.org/onlinedocs/gcc/Environment-Variables.html

我找不到任何build_ext文档,所以这里是命令行帮助

 Options for 'build_ext' command: --build-lib (-b) directory for compiled extension modules --build-temp (-t) directory for temporary files (build by-products) --plat-name (-p) platform name to cross-compile for, if supported (default: linux-x86_64) --inplace (-i) ignore build-lib and put compiled extensions into the source directory alongside your pure Python modules --include-dirs (-I) list of directories to search for header files (separated by ':') --define (-D) C preprocessor macros to define --undef (-U) C preprocessor macros to undefine --libraries (-l) external C libraries to link with --library-dirs (-L) directories to search for external C libraries (separated by ':') --rpath (-R) directories to search for shared C libraries at runtime --link-objects (-O) extra explicit link objects to include in the link --debug (-g) compile/link with debugging information --force (-f) forcibly build everything (ignore file timestamps) --compiler (-c) specify the compiler type --swig-cpp make SWIG create C++ files (default is C) --swig-opts list of SWIG command line options --swig path to the SWIG executable --user add user include, library and rpath --help-compiler list available compilers 

只是仅供参考…如果您在使用pip安装软件包时遇到困难,那么您可以使用

--no-clean选项,看看究竟发生了什么(也就是为什么构build不起作用)。 例如,如果numpy安装不正确,你可以尝试

pip install --no-clean numpy

然后看看临时文件夹,看看构build得到了多less。 在Windows机器上,这应该位于类似于:

C:\Users\Bob\AppData\Local\Temp\pip_build_Bob\numpy

要清楚的是,–no-clean选项会尝试安装该软件包,但不会自行清理,让您看到pip正在尝试执行什么操作。

否则,如果你只是想下载源代码,那么我会使用-d标志。 例如,要将Numpy源代码.tar文件下载到当前目录,请使用:

 pip install -d %cd% numpy 

基于Thorfin的回答,并假设你想要的包含和库位置在/ usr / local中,你可以像这样传递:

 sudo pip install --global-option=build_ext --global-option="-I/usr/local/include/" --global-option="-L/usr/local/lib" <you package name> 

你有没有使用过virtualenv ? 这是Python包,让你在一台机器上创build和维护多个独立的环境。 每个模块都可以使用不同的模块,而不会影响系统库或独立虚拟环境的依赖关系。

如果您没有root权限,则可以从源代码下载并使用virtualenv软件包 :

 $ curl -O https://pypi.python.org/packages/source/v/virtualenv/virtualenv-XXtar.gz $ tar xvfz virtualenv-XXtar.gz $ cd virtualenv-XX $ python virtualenv.py myVE 

我在本周末在Ubuntu Server 12.0.4上执行了上述步骤,并且完美运行。 您创build的每个新虚拟环境默认都带有PIP,因此将软件包安装到新环境中非常简单。

另一种表示包含文件和库的位置的方法是在运行pip之前设置相关的环境variables

 export LDFLAGS=-L/usr/local/opt/openssl/lib export CPPFLAGS=-I/usr/local/opt/openssl/include pip install cryptography 

以防万一有人帮忙,我仍然无法通过pipfind方法,所以最后只是简单地下载这个包,并通过它的'setup.py'来完成。 也切换到似乎更容易安装API称为“pymssql”。