使用 pip 将 Python 包安装到不同的目录中?

发布于 2024-09-02 17:08:02 字数 207 浏览 7 评论 0 原文

我知道明显的答案是使用 virtualenv 和 virtualenvwrapper,但由于各种原因我不能/不想这样做。

那么,如何修改命令

pip install package_name

以使 pip 将包安装在默认 site-packages 之外的位置呢?

I know the obvious answer is to use virtualenv and virtualenvwrapper, but for various reasons I can't/don't want to do that.

So how do I modify the command

pip install package_name

to make pip install the package somewhere other than the default site-packages?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(20

转身泪倾城 2024-09-09 17:08:03

使用 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.

德意的啸 2024-09-09 17:08:03
pip install "package_name" -t "target_dir"

来源 - https://pip.pypa.io/en/stable/reference/pip_install /

-t 开关=目标

pip install "package_name" -t "target_dir"

source - https://pip.pypa.io/en/stable/reference/pip_install/

-t switch = target

九八野马 2024-09-09 17:08:03

似乎没有人提到 -t 选项,但这是最简单的:

pip install -t <direct directory> <package>

Nobody seems to have mentioned the -t option but that the easiest:

pip install -t <direct directory> <package>
昔日梦未散 2024-09-09 17:08:03
pip install packageName -t pathOfDirectory

或者

pip install packageName --target pathOfDirectorty
pip install packageName -t pathOfDirectory

or

pip install packageName --target pathOfDirectorty
呢古 2024-09-09 17:08:03

只需在 @Ian Bicking 的答案中添加一点:

如果想将某些 Python 包安装到远程的主目录(没有 sudo 用户权限),则使用 --user 选项指定安装的目录也可以。服务器。

例如,

pip install --user python-memcached

该命令会将包安装到 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.,

pip install --user python-memcached

The command will install the package into one of the directories that listed in your PYTHONPATH.

孤檠 2024-09-09 17:08:03

较新版本的 pip(8 或更高版本)可以直接使用 --prefix选项

pip install --prefix=$PREFIX_PATH package_name

其中$PREFIX_PATH是放置lib、bin等顶级文件夹的安装前缀。

Newer versions of pip (8 or later) can directly use the --prefix option:

pip install --prefix=$PREFIX_PATH package_name

where $PREFIX_PATH is the installation prefix where lib, bin and other top-level folders are placed.

征﹌骨岁月お 2024-09-09 17:08:03

添加到已经很好的建议中,因为我在安装 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 设置了指令:

vim ~/.pydistutils.cfg

[install]
install-data=/usr/local/lib/python2.7
install-scripts=/usr/local/lib/python2.7/bin

然后(-I 用于强制安装,尽管以前失败/.local install):

pip install -I ipython

然后我添加了 /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:

vim ~/.pydistutils.cfg

[install]
install-data=/usr/local/lib/python2.7
install-scripts=/usr/local/lib/python2.7/bin

then (-I is used to force the install despite previous failures/.local install):

pip install -I ipython

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.

天生の放荡 2024-09-09 17:08:03

不幸的是,如果您将brew 与python 一起使用,pip/pip3 附带的选项非常有限。您没有如上所述的 --install-option、--target、--user 选项。

注意 pip install --user
对于酿造的 Python,普通的 pip install --user 被禁用。这是因为 distutils 中的一个错误,因为 Homebrew 编写了一个 distutils.cfg 来设置包前缀。
一个可能的解决方法(将可执行脚本放入 ~/Library/Python/./bin 中)是:
<代码>
python -m pip install --user --install-option="--prefix=" ;

你可能会发现这一行非常麻烦。我建议使用 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.

Note on pip install --user
The normal pip install --user is disabled for brewed Python. This is because of a bug in distutils, because Homebrew writes a distutils.cfg which sets the package prefix.
A possible workaround (which puts executable scripts in ~/Library/Python/./bin) is:

python -m pip install --user --install-option="--prefix=" <package-name>

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)

橙幽之幻 2024-09-09 17:08:03

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.

浮世清欢 2024-09-09 17:08:03

对于 Python v2.7.3 (GNU/Linux) 上的 pip v1.5.6,选项 --root 允许指定全局安装前缀,(显然)与特定包的选项无关。尝试一下,

$ pip install --root=/alternative/prefix/path package_name

With pip v1.5.6 on Python v2.7.3 (GNU/Linux), option --root allows to specify a global installation prefix, (apparently) irrespective of specific package's options. Try f.i.,

$ pip install --root=/alternative/prefix/path package_name
柠檬色的秋千 2024-09-09 17:08:03

我建议按照文档创建 ~/.pip/pip.conf 文件。请注意,文档中缺少指定的头目录,这导致以下错误:

error: install-base or install-platbase supplied, but installation scheme is incomplete

conf 文件的完整工作内容是:

[install]
install-base=$HOME
install-purelib=python/lib
install-platlib=python/lib.$PLAT
install-scripts=python/scripts
install-headers=python/include
install-data=python/data

不幸的是,我可以安装,但是当尝试卸载 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:

error: install-base or install-platbase supplied, but installation scheme is incomplete

The full working content of conf file is:

[install]
install-base=$HOME
install-purelib=python/lib
install-platlib=python/lib.$PLAT
install-scripts=python/scripts
install-headers=python/include
install-data=python/data

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.

阪姬 2024-09-09 17:08:03

system` 选项,它将把 pip package-bins 安装到所有用户都可以访问的 /usr/local/bin 。不使用此选项进行安装可能不适用于所有用户,因为内容会转到用户特定的目录,例如 $HOME/.local/bin,然后是用户特定的安装,必须为所有用户重复安装,如果未设置,也可能会出现路径问题对于用户来说,那么垃圾箱将不起作用。因此,如果您正在寻找所有用户 - 则需要具有 sudo 访问权限:

sudo su - 
python3 -m pip install --system <module>
logout
log back in 
which <module-bin> --> it should be installed on /usr/local/bin/

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:

sudo su - 
python3 -m pip install --system <module>
logout
log back in 
which <module-bin> --> it should be installed on /usr/local/bin/
巾帼英雄 2024-09-09 17:08:03

有时它仅适用于 Cache 参数

-m pip install -U pip --target=C:\xxx\python\lib\site-packages Pillow --cache-dir C:\tmp

Sometimes it works only works with Cache argument

-m pip install -U pip --target=C:\xxx\python\lib\site-packages Pillow --cache-dir C:\tmp
匿名的好友 2024-09-09 17:08:03

使用:

pip install package_name -t directory_path

如果出现以下错误:

错误:无法组合“--user”和“--target”

使用:

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:

ERROR: Can not combine '--user' and '--target'

Use:

pip install package_name -t directory_path --no-user

e.g. pip install pandas -t C:\Users\user\Desktop\Family\test --no-user

_蜘蛛 2024-09-09 17:08:03

使用默认的 venv,第三方 vitrualenv 或 virtualenvwrapper 将来会很痛苦

use default venv, third party vitrualenv or virtualenvwrapper will be pain in future

四叶草在未来唯美盛开 2024-09-09 17:08:02

--target 开关就是您要查找的内容for:

pip install --target d:\somewhere\other\than\the\default package_name

但您仍然需要将 d:\somewhere\other\than\the\default 添加到 PYTHONPATH 才能从该位置实际使用它们。

-t, --target <目录>
将软件包安装到

中。默认情况下,这不会替换 中的现有文件/文件夹。
使用 --upgrade 替换 中的现有软件包有新版本。


如果目标开关不可用,请升级 pip:

在 Linux 或 OS X 上:

pip install -U pip

在 Windows 上(这可以解决 一个问题):

python -m pip install -U pip

The --target switch is the thing you're looking for:

pip install --target d:\somewhere\other\than\the\default package_name

But you still need to add d:\somewhere\other\than\the\default to PYTHONPATH to actually use them from that location.

-t, --target <dir>
Install packages into <dir>. By default this will not replace existing files/folders in <dir>.
Use --upgrade to replace existing packages in <dir> with new versions.


Upgrade pip if target switch is not available:

On Linux or OS X:

pip install -U pip

On Windows (this works around an issue):

python -m pip install -U pip
诺曦 2024-09-09 17:08:02

使用:

pip install --install-option="--prefix=$PREFIX_PATH" package_name

您可能还想使用 --ignore-installed 强制使用此新前缀重新安装所有依赖项。您可以多次使用 --install-option 来添加可与 python setup.py install 一起使用的任何选项(--prefix 可能是您想要的,但是您可以使用更多选项)。

Use:

pip install --install-option="--prefix=$PREFIX_PATH" package_name

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 with python setup.py install (--prefix is probably what you want, but there are a bunch more options you could use).

紙鸢 2024-09-09 17:08:02

我发现设置 PYTHONUSERBASE 环境变量效果很好(来自 bug 关于这件事):(

PYTHONUSERBASE=/path/to/install/to pip install --user

或者在运行命令之前在您的环境中设置 PYTHONUSERBASE 目录,使用 export PYTHONUSERBASE=/path/to/install/to)

这使用了非常有用的 --user 选项,但告诉它创建 bin, libshare 以及您期望使用自定义前缀而不是 $HOME/.local 的其他目录。

然后您可以将其添加到您的 PATH 中,< code>PYTHONPATH 和其他变量,就像普通的安装目录一样。

请注意,如果依赖的任何软件包需要安装较新的版本,您可能还需要指定 --upgrade--ignore-installed 选项PYTHONUSERBASE 目录,以覆盖系统提供的版本。

完整示例

PYTHONUSERBASE=/opt/mysterypackage-1.0/python-deps pip install --user --upgrade numpy scipy

..安装 scipynumpy 软件包最新版本到一个目录中,然后您可以将其包含在您的 PYTHONPATH 中,如下所示(在本例中使用 bash 和 CentOS 6 上的 python 2.6):

export PYTHONPATH=/opt/mysterypackage-1.0/python-deps/lib64/python2.6/site-packages:$PYTHONPATH
export PATH=/opt/mysterypackage-1.0/python-deps/bin:$PATH

使用 virtualenv 仍然是一个更好、更简洁的解决方案!

Instead of the --target or --install-options options, I have found that setting the PYTHONUSERBASE environment variable works well (from discussion on a bug regarding this very thing):

PYTHONUSERBASE=/path/to/install/to pip install --user

(Or set the PYTHONUSERBASE directory in your environment before running the command, using export PYTHONUSERBASE=/path/to/install/to)

This uses the very useful --user option but tells it to make the bin, 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 the PYTHONUSERBASE directory, to override the system-provided versions.

A full example

PYTHONUSERBASE=/opt/mysterypackage-1.0/python-deps pip install --user --upgrade numpy scipy

..to install the scipy and numpy package most recent versions into a directory which you can then include in your PYTHONPATH like so (using bash and for python 2.6 on CentOS 6 for this example):

export PYTHONPATH=/opt/mysterypackage-1.0/python-deps/lib64/python2.6/site-packages:$PYTHONPATH
export PATH=/opt/mysterypackage-1.0/python-deps/bin:$PATH

Using virtualenv is still a better and neater solution!

我一向站在原地 2024-09-09 17:08:02

为了将库安装在我想要的位置,我使用终端导航到我想要的目录位置,然后使用

pip install mylibraryName -t . 

我从此页面获取的逻辑: 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

pip install mylibraryName -t . 

the logic of which I took from this page: https://cloud.google.com/appengine/docs/python/googlecloudstorageclient/download

薄荷港 2024-09-09 17:08:02

安装Python包通常只包含一些纯Python文件。如果包包含数据、脚本和/或可执行文件,它们将安装在与纯 Python 文件不同的目录中。

假设你的包没有数据/脚本/可执行文件,并且你希望你的 Python 文件进入 /python/packages/package_name (而不是低于 /python/packages 几层的子目录) 与使用 --prefix 时一样),您可以使用一次性命令:

pip install --install-option="--install-purelib=/python/packages" package_name

如果您希望所有(或大部分)包都到达那里,您可以编辑您的 ~ /.pip/pip.conf 包含:

[install]
install-option=--install-purelib=/python/packages

这样您就不会忘记必须一次又一次地指定它。

包中包含的任何可执行文件/数据/脚本仍将转到其默认位置,除非您指定其他安装选项(--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:

pip install --install-option="--install-purelib=/python/packages" package_name

If you want all (or most) of your packages to go there, you can edit your ~/.pip/pip.conf to include:

[install]
install-option=--install-purelib=/python/packages

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文