Install a Python package into a different directory using pip?

后端 未结 16 2423
借酒劲吻你
借酒劲吻你 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:10

    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.

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

    I found a simple way

    pip3 install "package_name" -t "target_dir"
    

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

    -t switch = target

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

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

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

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

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

    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.

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

    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.

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