Install a Python package into a different directory using pip?

后端 未结 16 2424
借酒劲吻你
借酒劲吻你 2020-11-22 05:55

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

相关标签:
16条回答
  • 2020-11-22 06:20

    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!

    0 讨论(0)
  • 2020-11-22 06:21

    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.

    0 讨论(0)
  • 2020-11-22 06:23

    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
    
    0 讨论(0)
  • 2020-11-22 06:26

    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)

    0 讨论(0)
提交回复
热议问题