如何在没有 pip 或 virtualenv 的情况下安装 python 包 [英] How can I install a python package without pip or virtualenv

查看:49
本文介绍了如何在没有 pip 或 virtualenv 的情况下安装 python 包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将一个 python 应用程序部署到我无法控制的生产服务器 (Ubuntu) 上,我也没有apt-get、pip、virtualenv、 等的权限.目前,它的服务器正在运行 python 2.6+.我需要安装 pycrypto 作为应用程序的依赖项,但鉴于我的权限有限,我不确定如何去做.我唯一有权做的就是 wget 一个资源并解压缩它或沿着这些路线进行的事情.

I have to deploy a python application to a production server (Ubuntu) that I do not control nor do I have permissions to apt-get, pip, virtualenv, etc. Currently, its the server is running python 2.6+. I need to install pycrypto as a dependency for the application but given my limited permissions, I'm not sure as to how to do it. The only think I have permissions to do is wget a resource and unpack it or things along those lines.

首先,是否可以在不通过上述方法安装的情况下使用它?如果没有,我可以下载这个包然后放入 pycrypto 目录中的 __init__.py 文件,这样 python 就知道如何像这样找到它:

First off, is it possible to use it without getting it installed in the aforementioned approach? If not, could I download the package then drop in __init__.py files in the pycrypto dir so python knows how to find it like so:

/my_app
  /pycrypto
     /__init__.py
     /pycrypto.py

推荐答案

根据 PEP370,从 python 2.6 开始,您可以拥有每个用户的站点目录(请参阅 Python 2.6 中有哪些新变化?).

According to PEP370, starting with python 2.6 you can have a per-user site directory (see the What's new in Python 2.6?).

因此您可以使用 easy_install--user 选项为每个用户而不是系统范围安装目录.我相信 pip 也存在类似的选项.这不需要任何权限,因为它只使用当前用户目录.

So you can use the --user option of easy_install to install the directory for each user instead of system-wide. I believe a similar option exists for pip too. This doesn't require any privileges since it only uses current user directories.

如果您没有安装任何安装程序,您可以手动将软件包解压到:

If you don't have any installer installed you can manually unpack the package into:

~/.local/lib/python2.6/site-packages

或者,如果您使用的是 Windows,请进入:

Or, if you are on Windows, into:

%APPDATA%/Python/Python26/site-packages

<小时>

对于pycrypto,该包需要在安装前构建,因为它包含一些C 代码.源应该包含一个 setup.py 文件.您必须构建运行的库


In the case of pycrypto, the package requires building before installation because it contains some C code. The sources should contain a setup.py file. You have to build the library running

python setup.py build

之后,您可以通过以下方式将其安装在用户目录中:

Afterwards you can install it in the user directory by giving:

python setup.py install --user

请注意,构建阶段可能需要安装一些 C 库.

Note that the building phase might require some C library to already be installed.

如果您不想这样做,only 选项是将库与您的应用程序一起发送.

If you don't want to do this, the only option is to ship the library together with your application.

顺便说一句:我相信 easy_install 在执行系统范围的安装之前并没有真正检查您是否是 root.它只是检查它是否可以写入系统范围的站点目录.因此,如果您确实有在那里编写的权限,那么首先就不需要使用 sudo.然而,这真的很奇怪......

By the way: I believe easy_install doesn't really check whether you are root before performing a system wide install. It simply checks whether it can write in the system-wide site directory. So, if you do have the privileges to write there, there's no need to use sudo in the first place. However this would be really odd...

这篇关于如何在没有 pip 或 virtualenv 的情况下安装 python 包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆