Install a Python package into a different director

2018-12-31 13:49发布

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?

标签: python pip
13条回答
美炸的是我
2楼-- · 2018-12-31 14:34

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.

查看更多
后来的你喜欢了谁
3楼-- · 2018-12-31 14:37

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
查看更多
人气声优
4楼-- · 2018-12-31 14:42

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

查看更多
初与友歌
5楼-- · 2018-12-31 14:46

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

查看更多
梦寄多情
6楼-- · 2018-12-31 14:48

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.

查看更多
琉璃瓶的回忆
7楼-- · 2018-12-31 14:48

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.

查看更多
登录 后发表回答