使用 pip 将 Python 包安装到不同的目录中?
我知道明显的答案是使用 virtualenv 和 virtualenvwrapper,但由于各种原因我不能/不想这样做。
那么,如何修改命令
pip install package_name
以使 pip
将包安装在默认 site-packages
之外的位置呢?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
使用 python3.5 和 pip 9.0.3 测试了这些选项:
pip install --target /myfolder [packages]
安装所有包,包括 /myfolder 下的依赖项。没有考虑到依赖包已经安装在Python的其他地方。您将从 /myfolder/[package_name] 中找到软件包。如果您有多个 Python 版本,则不会考虑这一点(包文件夹名称中没有 Python 版本)。
pip install --prefix /myfolder [packages]
检查依赖项是否已安装。将安装包到 /myfolder/lib/python3.5/site-packages/[packages]
pip install --root /myfolder [packages]
检查依赖项,如 --prefix 但安装位置将是 /myfolder /usr/local/lib/python3.5/site-packages/[package_name]。
pip install --user [packages]
将把软件包安装到 $HOME 中:
/home/[用户]/.local/lib/python3.5/site-packages
Python 会自动从此 .local 路径搜索,因此您无需将其放入 PYTHONPATH 中。
=>在大多数情况下 --user 是最好的选择。
如果由于某种原因无法使用主文件夹,则使用 --prefix。
Tested these options with python3.5 and pip 9.0.3:
pip install --target /myfolder [packages]
Installs ALL packages including dependencies under /myfolder. Does not take into account that dependent packages are already installed elsewhere in Python. You will find packages from /myfolder/[package_name]. In case you have multiple Python versions, this doesn't take that into account (no Python version in package folder name).
pip install --prefix /myfolder [packages]
Checks if dependencies are already installed. Will install packages into /myfolder/lib/python3.5/site-packages/[packages]
pip install --root /myfolder [packages]
Checks dependencies like --prefix but install location will be /myfolder/usr/local/lib/python3.5/site-packages/[package_name].
pip install --user [packages]
Will install packages into $HOME:
/home/[USER]/.local/lib/python3.5/site-packages
Python searches automatically from this .local path so you don't need to put it to your PYTHONPATH.
=> In most of the cases --user is the best option to use.
In case home folder can't be used because of some reason then --prefix.
来源 - https://pip.pypa.io/en/stable/reference/pip_install /
-t 开关=目标
source - https://pip.pypa.io/en/stable/reference/pip_install/
-t switch = target
似乎没有人提到 -t 选项,但这是最简单的:
Nobody seems to have mentioned the -t option but that the easiest:
或者
or
只需在 @Ian Bicking 的答案中添加一点:
如果想将某些 Python 包安装到远程的主目录(没有 sudo 用户权限),则使用
--user
选项指定安装的目录也可以。服务器。例如,
该命令会将包安装到 PYTHONPATH 中列出的目录之一。
Just add one point to @Ian Bicking's answer:
Using the
--user
option to specify the installed directory also work if one wants to install some Python package into one's home directory (without sudo user right) on remote server.E.g.,
The command will install the package into one of the directories that listed in your PYTHONPATH.
较新版本的
pip
(8 或更高版本)可以直接使用--prefix
选项:其中
$PREFIX_PATH
是放置lib、bin等顶级文件夹的安装前缀。Newer versions of
pip
(8 or later) can directly use the--prefix
option:where
$PREFIX_PATH
is the installation prefix where lib, bin and other top-level folders are placed.添加到已经很好的建议中,因为我在安装 IPython 时遇到了问题,而我没有对
/usr/local
的写入权限。pip 使用 distutils 进行安装,此线程讨论了这如何导致问题依赖于 sys.prefix 设置。
当 IPython 安装尝试写入“/usr/local/share/man/man1”且权限被拒绝时,我的问题发生了。由于安装失败,它似乎没有在 bin 目录中写入 IPython 文件。
使用“--user”有效并将文件写入 ~/.local。将 ~/.local/bin 添加到 $PATH 意味着我可以从那里使用“ipython”。
但是,我正在尝试为许多用户安装此程序,并且已被授予对
/usr/local/lib/python2.7
目录的写入权限。我在那里创建了一个“bin”目录并为 distutils 设置了指令:然后(
-I
用于强制安装,尽管以前失败/.local install):然后我添加了
/usr/ local/lib/python2.7/bin
到$PATH
。我想我应该包括这个,以防其他人在他们没有 sudo 访问权限的机器上遇到类似的问题。
To add to the already good advice, as I had an issue installing IPython when I didn't have write permissions to
/usr/local
.pip uses distutils to do its install and this thread discusses how that can cause a problem as it relies on the
sys.prefix
setting.My issue happened when the IPython install tried to write to '/usr/local/share/man/man1' with Permission denied. As the install failed it didn't seem to write the IPython files in the bin directory.
Using "--user" worked and the files were written to ~/.local. Adding ~/.local/bin to the $PATH meant I could use "ipython" from there.
However I'm trying to install this for a number of users and had been given write permission to the
/usr/local/lib/python2.7
directory. I created a "bin" directory under there and set directives for distutils:then (
-I
is used to force the install despite previous failures/.local install):Then I added
/usr/local/lib/python2.7/bin
to$PATH
.I thought I'd include this in case anyone else has similar issues on a machine they don't have sudo access to.
不幸的是,如果您将brew 与python 一起使用,pip/pip3 附带的选项非常有限。您没有如上所述的 --install-option、--target、--user 选项。
你可能会发现这一行非常麻烦。我建议使用 pyenv 进行管理。
如果您使用的是
brew Upgrade python python3
讽刺的是,您实际上是在降级 pip 功能。
(我发布这个答案,只是因为我的 mac osx 中的 pip 没有 --target 选项,而且我花了几个小时修复它)
If you are using brew with python, unfortunately, pip/pip3 ships with very limited options. You do not have --install-option, --target, --user options as mentioned above.
You might find this line very cumbersome. I suggest use pyenv for management.
If you are using
brew upgrade python python3
Ironically you are actually downgrade pip functionality.
(I post this answer, simply because pip in my mac osx does not have --target option, and I have spent hours fixing it)
pip install /path/to/package/
现在可以了。
与此和使用
-e
或--editable
标志的区别在于-e
链接到保存包的位置(即您的下载文件夹),而不是将其安装到您的 python 路径中。这意味着如果您删除该包/将其移动到另一个文件夹,您将无法使用它。
pip install /path/to/package/
is now possible.
The difference with this and using the
-e
or--editable
flag is that-e
links to where the package is saved (i.e. your downloads folder), rather than installing it into your python path.This means if you delete/move the package to another folder, you won't be able to use it.
对于 Python
v2.7.3
(GNU/Linux) 上的 pipv1.5.6
,选项--root
允许指定全局安装前缀,(显然)与特定包的选项无关。尝试一下,With pip
v1.5.6
on Pythonv2.7.3
(GNU/Linux), option--root
allows to specify a global installation prefix, (apparently) irrespective of specific package's options. Try f.i.,我建议按照文档创建 ~/.pip/pip.conf 文件。请注意,文档中缺少指定的头目录,这导致以下错误:
conf 文件的完整工作内容是:
不幸的是,我可以安装,但是当尝试卸载 pip 时,告诉我没有这样的软件包用于卸载过程... 。所以仍然有问题,但包转到了预定义的位置。
I suggest to follow the documentation and create ~/.pip/pip.conf file. Note in the documentation there are missing specified header directory, which leads to following error:
The full working content of conf file is:
Unfortunatelly I can install, but when try to uninstall pip tells me there is no such package for uninstallation process.... so something is still wrong but the package goes to its predefined location.
system` 选项,它将把 pip package-bins 安装到所有用户都可以访问的 /usr/local/bin 。不使用此选项进行安装可能不适用于所有用户,因为内容会转到用户特定的目录,例如 $HOME/.local/bin,然后是用户特定的安装,必须为所有用户重复安装,如果未设置,也可能会出现路径问题对于用户来说,那么垃圾箱将不起作用。因此,如果您正在寻找所有用户 - 则需要具有 sudo 访问权限:
system` option, that will install pip package-bins to /usr/local/bin thats accessible to all users. Installing without this option may not work for all users as things go to user specific dir like $HOME/.local/bin and then it is user specific install which has to be repeated for all users, also there can be path issues if not set for users, then bins won't work. So if you are looking for all users - yu need to have sudo access:
有时它仅适用于 Cache 参数
Sometimes it works only works with Cache argument
使用:
pip install package_name -t directory_path
如果出现以下错误:
使用:
pip install package_name -t directory_path --no-user
eg
pip install pandas -t C:\Users\user\Desktop\Family\test --无用户
Use:
pip install package_name -t directory_path
If you get the following error:
Use:
pip install package_name -t directory_path --no-user
e.g.
pip install pandas -t C:\Users\user\Desktop\Family\test --no-user
使用默认的 venv,第三方 vitrualenv 或 virtualenvwrapper 将来会很痛苦
use default venv, third party vitrualenv or virtualenvwrapper will be pain in future
--target 开关就是您要查找的内容for:
但您仍然需要将 d:\somewhere\other\than\the\default 添加到
PYTHONPATH
才能从该位置实际使用它们。如果目标开关不可用,请升级 pip:
在 Linux 或 OS X 上:
在 Windows 上(这可以解决 一个问题):
The --target switch is the thing you're looking for:
But you still need to add
d:\somewhere\other\than\the\default
toPYTHONPATH
to actually use them from that location.Upgrade pip if target switch is not available:
On Linux or OS X:
On Windows (this works around an issue):
使用:
您可能还想使用
--ignore-installed
强制使用此新前缀重新安装所有依赖项。您可以多次使用--install-option
来添加可与python setup.py install
一起使用的任何选项(--prefix
可能是您想要的,但是您可以使用更多选项)。Use:
You might also want to use
--ignore-installed
to force all dependencies to be reinstalled using this new prefix. You can use--install-option
to multiple times to add any of the options you can use withpython setup.py install
(--prefix
is probably what you want, but there are a bunch more options you could use).我发现设置
PYTHONUSERBASE
环境变量效果很好(来自 bug 关于这件事):(或者在运行命令之前在您的环境中设置
PYTHONUSERBASE
目录,使用export PYTHONUSERBASE=/path/to/install/to
)这使用了非常有用的
--user
选项,但告诉它创建bin
,lib
、share
以及您期望使用自定义前缀而不是$HOME/.local
的其他目录。然后您可以将其添加到您的
PATH
中,< code>PYTHONPATH 和其他变量,就像普通的安装目录一样。请注意,如果依赖的任何软件包需要安装较新的版本,您可能还需要指定
--upgrade
和--ignore-installed
选项PYTHONUSERBASE
目录,以覆盖系统提供的版本。完整示例
..安装
scipy
和numpy
软件包最新版本到一个目录中,然后您可以将其包含在您的PYTHONPATH
中,如下所示(在本例中使用 bash 和 CentOS 6 上的 python 2.6):使用 virtualenv 仍然是一个更好、更简洁的解决方案!
Instead of the
--target
or--install-options
options, I have found that setting thePYTHONUSERBASE
environment variable works well (from discussion on a bug regarding this very thing):(Or set the
PYTHONUSERBASE
directory in your environment before running the command, usingexport PYTHONUSERBASE=/path/to/install/to
)This uses the very useful
--user
option but tells it to make thebin
,lib
,share
and other directories you'd expect under a custom prefix rather than$HOME/.local
.Then you can add this to your
PATH
,PYTHONPATH
and other variables as you would a normal installation directory.Note that you may also need to specify the
--upgrade
and--ignore-installed
options if any packages upon which this depends require newer versions to be installed in thePYTHONUSERBASE
directory, to override the system-provided versions.A full example
..to install the
scipy
andnumpy
package most recent versions into a directory which you can then include in yourPYTHONPATH
like so (using bash and for python 2.6 on CentOS 6 for this example):Using virtualenv is still a better and neater solution!
为了将库安装在我想要的位置,我使用终端导航到我想要的目录位置,然后使用
我从此页面获取的逻辑: https://cloud.google.com/appengine/docs/python/googlecloudstorageclient/download
To pip install a library exactly where I wanted it, I navigated to the location I wanted the directory with the terminal then used
the logic of which I took from this page: https://cloud.google.com/appengine/docs/python/googlecloudstorageclient/download
安装Python包通常只包含一些纯Python文件。如果包包含数据、脚本和/或可执行文件,它们将安装在与纯 Python 文件不同的目录中。
假设你的包没有数据/脚本/可执行文件,并且你希望你的 Python 文件进入
/python/packages/package_name
(而不是低于/python/packages 几层的子目录)
与使用--prefix
时一样),您可以使用一次性命令:如果您希望所有(或大部分)包都到达那里,您可以编辑您的
~ /.pip/pip.conf
包含:这样您就不会忘记必须一次又一次地指定它。
包中包含的任何可执行文件/数据/脚本仍将转到其默认位置,除非您指定其他安装选项(
--prefix
/--install-data
/--install-scripts
等,详细信息请参见 自定义安装选项)。Installing a Python package often only includes some pure Python files. If the package includes data, scripts and or executables, these are installed in different directories from the pure Python files.
Assuming your package has no data/scripts/executables, and that you want your Python files to go into
/python/packages/package_name
(and not some subdirectory a few levels below/python/packages
as when using--prefix
), you can use the one time command:If you want all (or most) of your packages to go there, you can edit your
~/.pip/pip.conf
to include:That way you can't forget about having to specify it again and again.
Any excecutables/data/scripts included in the package will still go to their default places unless you specify addition install options (
--prefix
/--install-data
/--install-scripts
, etc., for details look at the custom installation options).