0

我不想使用简单安装、站点包中的符号链接或 PYTHONPATH 来安装 python 模块。
所以,我正在尝试一些我确实想要系统范围的东西,然后任何应用程序安装都在本地完成。请注意,此处只需要 root 密码一次。

首先创建一个符号链接.../pythonX.Y/site-packages/mymodules -> /home/me/lib/python_related

所以,我创建了一个名为

/home/me/lib/python_related/

在那里:

/home/me/lib/python_related
/home/me/lib/python_related/__init__.py
/home/me/lib/python_related/django_related/
/home/me/lib/python_related/django_related/core
/home/me/lib/python_related/django_related/core/Django1.0
/home/me/lib/python_related/django_related/core/Django1.1
/home/me/lib/python_related/django_related/core/mycurrent_django -> Django1.1/django
/home/me/lib/python_related/django_related/apps
/home/me/lib/python_related/django_related/apps/tagging
/home/me/lib/python_related/django_related/apps/tagging/django-tagging-0.2
/home/me/lib/python_related/django_related/apps/tagging/django-tagging-0.3
/home/me/lib/python_related/django_related/apps/tagging/mycurrent_tagging -> django-tagging-0.3

现在,这里是内容:

/home/me/lib/python_related/__init__.py

==========================================
import sys, os

# tell us where you keep all your modules and this didn't work as it gave me
# the location of the site-packages
#PYTHON_MODULE_PATH = os.path.dirname(__file__)
PYTHON_MODULE_PATH = "/home/me/libs/python_bucket"

def run_cmd(cmd):
        """
        Given a command name, this function will run the command and returns the output
        in a list.
        """
        output = []
        phdl = os.popen(cmd)
        while 1:
                line = phdl.readline()
                if line == "":
                        break
                output.append(line.replace("\n", ""))
        return output

def install():
        """
        A cheesy way of installing and managing your python apps locally without
        a need to install them in the site-package. All you'd need is to install
        the directory containing this file in the site-package and that's it.
        Anytime you have a python package you want to install, just put it in a
        proper sub-directory and make a symlink to that directory called mycurrent_xyz
        and you are done. (e.g. mycurrent_django, mycurrent_tagging .. etc)
        """
        cmd = "find %s -name mycurrent_*" % PYTHON_MODULE_PATH
        modules_to_be_installed = run_cmd(cmd)
        sys.path += modules_to_be_installed

install()
=======================================================

现在在任何新的 python 项目中,只需导入您的 mymodules 并使用正确的符号链接拉入上述目录中的任何应用程序。这样,您可以拥有多个应用程序副本,只需将 mycurrent_xyz 用于您要使用的应用程序。

现在这是一个问题。这是一个好方法吗?

4

1 回答 1

4

看看virtualenv

它可能会做你所追求的。

于 2009-09-08T04:19:34.847 回答