如何在没有root权限的情况下安装python包?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14179941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:41:16  来源:igfitidea点击:

How to install python packages without root privileges?

pythonnumpyinstallationscipy

提问by Skyler

I am using numpy/?scipy/ pynestto do some research computing on Mac OS X. For performance, we rent a 400-node cluster (with Linux) from our university so that the tasks could be done parallel. The problem is that we are NOT allowed to install any extra packages on the cluster (no sudoor any installation tool), they only provide the raw python itself.

我正在使用numpy/? scipy/pynest在 Mac OS X 上做一些研究计算。为了性能,我们从我们大学租了一个 400 节点的集群(使用 Linux),以便任务可以并行完成。问题是我们不允许在集群上安装任何额外的包(没有sudo或任何安装工具),它们只提供原始 python 本身。

How can I run my scripts on the cluster then? Is there any way to integrate the modules (numpy and scipy also have some compiled binaries I think) so that it could be interpreted and executed without installing packages?

那么我如何在集群上运行我的脚本?有什么方法可以集成模块(我认为 numpy 和 scipy 也有一些已编译的二进制文件),以便可以在不安装软件包的情况下对其进行解释和执行?

采纳答案by Colonel Panic

You don't need root privileges to install packages in your home directory. You can do that with a command such as

您不需要 root 权限即可在您的主目录中安装软件包。你可以用一个命令来做到这一点,例如

pip install --user numpy

or from source

或从源头

python setup.py install --user

See https://stackoverflow.com/a/7143496/284795

https://stackoverflow.com/a/7143496/284795



The first alternative is much more convenient, so if the server doesn't have pipor easy_install, you should politely ask the admins to add it, explaining the benefit to them (they won't be bothered anymore by requests for individual packages).

第一种选择要方便得多,所以如果服务器没有pipor easy_install,你应该礼貌地要求管理员添加它,向他们解释好处(他们不会再被单个包的请求所困扰)。

回答by David Robinson

You could create a virtual environment through the virtualenvpackage.

您可以通过virtualenv包创建一个虚拟环境。

This creates a folder (say venv) with a new copy of the Python executable and a new site-packagesdirectory, into which you can "install" any number of packages without needing any kind of administrative access at all. Thus, activating the environment through source venv/bin/activatewill give Python an environment that's equivalent to having those packages installed.

这将创建一个文件夹(比如venv),其中包含 Python 可执行文件的新副本和一个新site-packages目录,您可以在其中“安装”任意数量的包,而根本不需要任何类型的管理访问权限。因此,通过激活环境source venv/bin/activate将为 Python 提供一个相当于安装这些包的环境。

I know this works for SGE clusters, although how the virtual environment is activated might depend on your cluster's configuration.

我知道这适用于 SGE 集群,尽管虚拟环境的激活方式可能取决于您的集群配置。

You can try installing virtualenvon your cluster within your own site-packages directory using the following steps:

您可以尝试virtualenv使用以下步骤在您自己的站点包目录中的集群上安装:

  1. Download virtualenv from here, put it on your cluster

  2. Install it using setup.pyto a specific, local directory to serve as your own site-packages:

    python setup.py build
    python setup.py install --install-base /path/to/local-site-packages
    
  3. Add that directory to your PYTHONPATH:

    export PYTHONPATH="/path/to/local-site-packages:${PYTHONPATH}"
    
  4. Create a virtualenv:

    virtualenv venv
    
  1. 这里下载 virtualenv ,把它放在你的集群上

  2. 将其安装setup.py到特定的本地目录以用作您自己的站点包:

    python setup.py build
    python setup.py install --install-base /path/to/local-site-packages
    
  3. 将该目录添加到您的 PYTHONPATH:

    export PYTHONPATH="/path/to/local-site-packages:${PYTHONPATH}"
    
  4. 创建一个虚拟环境:

    virtualenv venv
    

回答by john graham

You can import a module from an arbitrary path by calling:

您可以通过调用从任意路径导入模块:

sys.path.append()

sys.path.append()

回答by Brian Cain

The Python Distribution Anaconda solves many of the issues discussed in this questions. Anaconda does not require Admin or root access and is able to install to your home directory. Anaconda comes with many of the packages in question (scipy, numpy, sklearn, etc...) as well as the conda installer to install additional packages should additional ones be necessary.

Python Distribution Anaconda 解决了这个问题中讨论的许多问题。Anaconda 不需要管理员或 root 访问权限,并且能够安装到您的主目录。Anaconda 附带了许多有问题的软件包(scipy、numpy、sklearn 等)以及 conda 安装程序,用于安装其他软件包(如果需要其他软件包)。

It can be downloaded from https://www.continuum.io/downloads

可以从https://www.continuum.io/downloads下载