python pip 指定一个库目录和一个包含目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18783390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
python pip specify a library directory and an include directory
提问by Cricri
I am using pip and trying to install a python module called pyodbc which has some dependencies on non-python libraries like unixodbc-dev, unixodbc-bin, unixodbc. I cannot install these dependencies system wide at the moment, as I am only playing, so I have installed them in a non-standard location. How do I tell pip where to look for these dependencies ? More exactly, how do I pass information through pip of include dirs (gcc -I) and library dirs (gcc -L -l) to be used when building the pyodbc extension ?
我正在使用 pip 并尝试安装一个名为 pyodbc 的 python 模块,该模块对 unixodbc-dev、unixodbc-bin、unixodbc 等非 python 库有一些依赖性。我目前无法在系统范围内安装这些依赖项,因为我只是在玩,所以我将它们安装在非标准位置。我如何告诉 pip 在哪里寻找这些依赖项?更确切地说,如何通过包含目录(gcc -I)和库目录(gcc -L -l)的 pip 传递信息以在构建 pyodbc 扩展时使用?
回答by Cricri
Just in case it's of help to somebody, I still could not find a way to do it through pip, so ended up simply downloading the package and doing through its 'setup.py'. Also switched to what seems an easier to install API called 'pymssql'.
以防万一它对某人有帮助,我仍然找不到通过 pip 执行此操作的方法,因此最终只是下载了该软件包并通过其“setup.py”进行操作。还切换到看起来更容易安装的 API,称为“pymssql”。
回答by James Nicholson
Have you ever used virtualenv? It's Python package that let's you create and maintain multiple isolated environments on one machine. Each can use different modules independent of one another without screwing up dependencies in your system library or a separate virtual environment.
你用过virtualenv吗?它是 Python 包,可让您在一台机器上创建和维护多个隔离的环境。每个都可以使用彼此独立的不同模块,而不会破坏系统库或单独的虚拟环境中的依赖关系。
If you don't have root privileges, you can download and use the virtualenv package from source:
如果您没有 root 权限,您可以从源下载并使用 virtualenv 包:
$ curl -O https://pypi.python.org/packages/source/v/virtualenv/virtualenv-X.X.tar.gz
$ tar xvfz virtualenv-X.X.tar.gz
$ cd virtualenv-X.X
$ python virtualenv.py myVE
I followed the above steps this weekend on Ubuntu Server 12.0.4 and it worked perfectly. Each new virtual environment you create comes with PIP by default so installing packages into your new environment is easy.
本周末我在 Ubuntu Server 12.0.4 上遵循了上述步骤,并且运行良好。默认情况下,您创建的每个新虚拟环境都带有 PIP,因此将软件包安装到新环境中很容易。
回答by Thorfin
pip has a --global-option
flag
pip 有一个--global-option
标志
You can use it to pass additional flags to build_ext
.
For instance, to add a --library-dirs (-L) flag:pip install --global-option=build_ext --global-option="-L/path/to/local" pyodbc
您可以使用它向build_ext
.
例如,要添加 --library-dirs (-L) 标志:pip install --global-option=build_ext --global-option="-L/path/to/local" pyodbc
gcc supports also environment variables: http://gcc.gnu.org/onlinedocs/gcc/Environment-Variables.html
gcc 还支持环境变量:http: //gcc.gnu.org/onlinedocs/gcc/Environment-Variables.html
I couldn't find any build_ext documentation, so here is the command line help
我找不到任何 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
回答by bremen_matt
Just FYI... If you are having trouble installing a package with pip, then you can use the
仅供参考...如果您在使用 pip 安装软件包时遇到问题,那么您可以使用
--no-clean
option to see what is exactly going on (that is, why the build did not work). For instance, if numpy is not installing properly, you could try
--no-clean
选项以查看究竟发生了什么(即为什么构建不起作用)。例如,如果 numpy 没有正确安装,你可以尝试
pip install --no-clean numpy
pip install --no-clean numpy
then look at the Temporary folder to see how far the build got. On a Windows machine, this should be located at something like:
然后查看 Temporary 文件夹以查看构建完成了多远。在 Windows 机器上,它应该位于如下位置:
C:\Users\Bob\AppData\Local\Temp\pip_build_Bob\numpy
C:\Users\Bob\AppData\Local\Temp\pip_build_Bob\numpy
Just to be clear, the --no-clean option tries to install the package, but does not clean up after itself, letting you see what pip was trying to do.
需要明确的是,--no-clean 选项会尝试安装软件包,但不会自行清理,让您了解 pip 尝试执行的操作。
Otherwise, if you just want to download the source code, then I would use the -d
flag. For instance, to download the Numpy source code .tar
file to the current directory, use:
否则,如果您只想下载源代码,那么我会使用该-d
标志。例如,要将 Numpy 源代码.tar
文件下载到当前目录,请使用:
pip install -d %cd% numpy
回答by viathon
Building on Thorfin's answer and assuming that your desired include and library locations are in /usr/local, you can pass both in like so:
基于 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>
回答by Huy Le
Another way to indicate the location of include files and libraries are set relevant environment variables before running pip e.g.
另一种指示包含文件和库位置的方法是在运行 pip 之前设置相关的环境变量,例如
export LDFLAGS=-L/usr/local/opt/openssl/lib
export CPPFLAGS=-I/usr/local/opt/openssl/include
pip install cryptography
回答by kite
I was also helped by Thorfin's answer; I was building GTK3+ on windows and installing pygobject, I was having difficulties on how to include multiple folders with pip install.
Thorfin 的回答也帮助了我;我正在 Windows 上构建 GTK3+ 并安装 pygobject,我在如何使用 pip install 包含多个文件夹方面遇到了困难。
I tried creating pip config file as per pip documentation. but failed. the one working is with the command line:
我尝试根据 pip 文档创建 pip 配置文件。但失败了。一个工作是使用命令行:
--global-option=build_ext followed by --global-option="-IlistOfDirectories" and/or --global-option="-LlistofDirectories"
--global-option=build_ext 后跟 --global-option="-IlistOfDirectories" 和/或 --global-option="-LlistofDirectories"
the separator that works with multiple folders in windows is ';' semicolon, NOT colon ':' it might be different in other OS.
在 Windows 中处理多个文件夹的分隔符是';' 分号,而不是冒号 ':' 在其他操作系统中可能会有所不同。
sample working command line:
示例工作命令行:
pip install --global-option=build_ext --global-option="-Ic:/gtk-build/gtk/x64/release/include;d:/gtk-build/gtk/x64/release/include/gobject-introspection-1.0" --global-option="-Lc:\gtk-build\gtk\x64\release\lib" pygobject==3.27.1
you can use '\' or '/' for path, but make sure do not type backslash next to "
您可以使用“\”或“/”作为路径,但请确保不要在“
this below will fail because there is backslash next to double quote
下面这将失败,因为双引号旁边有反斜杠
pip install --global-option=build_ext --global-option="-Ic:\willFail\" --global-option="-Lc:\willFail\" pygobject==3.27.1