问题:使用pip将Python软件包安装到其他目录中吗?
我知道明显的答案是使用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
?
回答 0
采用:
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).
回答 1
该–target开关是你正在寻找的东西:
pip install --target=d:\somewhere\other\than\the\default package_name
但是您仍然需要添加d:\somewhere\other\than\the\default
到PYTHONPATH
该位置才能实际使用它们。
-t,–target <dir>
将软件包安装到<dir>中。默认情况下,这不会替换<dir>中的现有文件/文件夹。
使用–upgrade将<dir>中的现有软件包替换为新版本。
如果目标开关不可用,请升级点子:
在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
回答 2
代替--target
选项或--install-options
选项,我发现以下方法很好用(来自有关此问题的错误讨论,网址为https://github.com/pypa/pip/issues/446):
PYTHONUSERBASE=/path/to/install/to pip install --user
(或者PYTHONUSERBASE
在运行命令之前使用,在您的环境中设置目录export PYTHONUSERBASE=/path/to/install/to
)
它使用了非常有用的--user
选项,但告诉它使bin
,lib
,share
你会在一个自定义的前缀期待,而不是和其他目录$HOME/.local
。
然后,你可以到你添加这个PATH
,PYTHONPATH
你将一个正常的安装目录和其他变量。
请注意,如果依赖于此的任何软件包都需要在目录中安装较新的版本,则您可能还需要指定--upgrade
和--ignore-installed
选项PYTHONUSERBASE
,以覆盖系统提供的版本。
一个完整的例子:
PYTHONUSERBASE=/opt/mysterypackage-1.0/python-deps pip install --user --upgrade numpy scipy
..将最新版本的scipy
并将numpy
最新版本安装并打包到一个目录中,然后可以将其包含在目录中PYTHONPATH
(对于本示例,在CentOS 6上使用bash和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
option or the --install-options
option, I have found that the following works well (from discussion on a bug regarding this very thing at https://github.com/pypa/pip/issues/446):
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!
回答 3
安装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).
回答 4
回答 5
似乎没有人提到-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>
回答 6
只需在@Ian Bicking的答案中添加一点:
--user
如果要在远程服务器上将一些Python软件包安装到其主目录中(没有sudo用户权限),则使用该选项指定已安装目录也可以使用。
例如,
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.
回答 7
使用python3.5和pip 9.0.3测试了这些选项:
pip install –target / myfolder [程序包]
在/ myfolder下安装所有软件包,包括依赖项。不考虑依赖包已在Python中的其他位置安装。您可以从/ myfolder / [package_name]中找到软件包。如果您有多个Python版本,则无需考虑(软件包文件夹名称中没有Python版本)。
pip install –prefix / myfolder [软件包]
检查是否已安装依赖项。将安装包到/myfolder/lib/python3.5/site-packages/[packages]
pip install –root / myfolder [软件包]
检查–prefix之类的依赖项,但安装位置将为/myfolder/usr/local/lib/python3.5/site-packages/[package_name]。
pip install –user [软件包]
会将软件包安装到$ HOME中:/home/[USER]/.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 are dependencies 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.
回答 8
较新的版本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.
回答 9
pip install packageName -t pathOfDirectory
要么
pip install packageName --target pathOfDirectorty
pip install packageName -t pathOfDirectory
or
pip install packageName --target pathOfDirectorty
回答 10
补充一下已经很好的建议,因为在没有的写入权限时安装IPython时遇到了问题/usr/local
。
pip使用distutils进行安装,该线程讨论了依赖sys.prefix
设置会导致问题的原因。
我的问题发生在IPython安装尝试在权限被拒绝的情况下写入’/ usr / local / share / man / man1’时。由于安装失败,因此似乎没有在bin目录中写入IPython文件。
使用“ –user”可以将文件写入〜/ .local。在$ PATH中添加〜/ .local / bin意味着我可以从那里使用“ 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安装):
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.
回答 11
不幸的是,如果您将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=" <package-name>
您可能会发现此行非常麻烦。我建议使用pyenv进行管理。如果您正在使用
brew upgrade python python3
具有讽刺意味的是,您实际上是在降低点子功能。
(我之所以发布此答案,仅仅是因为我的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)
回答 12
v1.5.6
在Python v2.7.3
(GNU / Linux)上使用pip 时,option --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
回答 13
我建议遵循文档并创建〜/ .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.
回答 14
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.
回答 15