views:

143

answers:

1

Hi,

I didn't want to install python modules using easy install, symlinks in site-packages or PYTHONPATH.
So, I am trying something that I do wants system wide, then any application installation is done locally. Note, the root password is required only once here.

First create a symblink of.../pythonX.Y/site-packages/mymodules -> /home/me/lib/python_related

So, I create a directory called

/home/me/lib/python_related/

In there:

/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

Now, here is the content of:

/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()
=======================================================

Now in any new python project, just import your mymodules and that pulls in any apps that you have in the above directory with the proper symbolic link. This way you can have multiple copies of apps and just use the mycurrent_xyz to the one you want to use.

Now here is question. Is this a good way of doing it?

+4  A: 

Have a look at virtualenv.

It may do what you are after.

Matthew Schinckel