Not Sure This Can Be Done...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gamename

    Not Sure This Can Be Done...

    Hi,

    I generally have several copies of the same development environment
    checked out from cvs at any one time. Each development tree has a
    'tools' dir containing python modules. Scattered in different places
    in the tree are various python scripts.

    What I want to do is force my scripts to always look in the closest
    'tools' dir for any custom modules to import. For example:

    tree1/tools
    tree1/my_scripts/foo.py

    tree2/tools
    tree2/my_scripts/foo.py

    How can I make 'foo.py' always look in '../../tools' for custom
    modules? My preference would be to avoid changing the 'foo.py' script
    and have some way to resolve it via the environment (like PYTHONPATH,
    or .pth files, etc.).

    Anyone have any ideas?
    TIA,
    -T

  • Diez B. Roggisch

    #2
    Re: Not Sure This Can Be Done...

    gamename schrieb:
    Hi,
    >
    I generally have several copies of the same development environment
    checked out from cvs at any one time. Each development tree has a
    'tools' dir containing python modules. Scattered in different places
    in the tree are various python scripts.
    >
    What I want to do is force my scripts to always look in the closest
    'tools' dir for any custom modules to import. For example:
    >
    tree1/tools
    tree1/my_scripts/foo.py
    >
    tree2/tools
    tree2/my_scripts/foo.py
    >
    How can I make 'foo.py' always look in '../../tools' for custom
    modules? My preference would be to avoid changing the 'foo.py' script
    and have some way to resolve it via the environment (like PYTHONPATH,
    or .pth files, etc.).
    >
    Anyone have any ideas?

    Use virtualenv to create a local python, and activate that when
    developing for that branch.

    Alternatively, you can of course manipulate the PYTHONPATH yourself -
    but why should you if virtualenv takes care of that for you?

    Diez

    Comment

    • gamename

      #3
      Re: Not Sure This Can Be Done...

      Use virtualenv to create a local python, and activate that when
      developing for that branch.
      Thanks for the suggestion, but that's the problem: having to activate
      it.
      Isn't there some way to simply have the local script look at a
      specified
      dir rather than starting a virtual environment?

      -T

      Comment

      • Diez B. Roggisch

        #4
        Re: Not Sure This Can Be Done...

        gamename schrieb:
        >Use virtualenv to create a local python, and activate that when
        >developing for that branch.
        >
        Thanks for the suggestion, but that's the problem: having to activate
        it.
        Isn't there some way to simply have the local script look at a
        specified
        dir rather than starting a virtual environment?
        You can only do that yourself - you can write a helper-module that will
        inspect the environment, locates the module, and appends it path to
        sys.path.

        Diez

        Comment

        • MrJean1

          #5
          Re: Not Sure This Can Be Done...

          In any script upon startup, sys.path[0] contains the full path of the
          directory where the script is located. See <http://docs.python.org/
          lib/module-sys.htmlunder 'path'.

          it should be straightforward from here (untested though). In each
          script, get the sys.path[0] string, split it using os.path, replace
          the last item with 'tools' and join again with os.path. If the
          resulting string is not in sys.path, insert it after sys.path[0].

          /Jean Brouwers


          On Apr 1, 1:57 pm, gamename <namesagame-use...@yahoo.co mwrote:
          Hi,
          >
          I generally have several copies of the same development environment
          checked out from cvs at any one time.  Each development tree has a
          'tools' dir containing python modules.  Scattered in different places
          in the tree are various python scripts.
          >
          What I want to do is force my scripts to always look in the closest
          'tools' dir for any custom modules to import. For example:
          >
          tree1/tools
          tree1/my_scripts/foo.py
          >
          tree2/tools
          tree2/my_scripts/foo.py
          >
          How can I make 'foo.py' always look in '../../tools' for custom
          modules? My preference would be to avoid changing the 'foo.py' script
          and have some way to resolve it via the environment (like PYTHONPATH,
          or .pth files, etc.).
          >
          Anyone have any ideas?
          TIA,
          -T

          Comment

          • gamename

            #6
            Re: Not Sure This Can Be Done...

            Thanks! Good suggestion(s) all.

            Maybe I can get this done. :)

            -T

            On Apr 1, 4:49 pm, MrJean1 <MrJe...@gmail. comwrote:
            In any script upon startup, sys.path[0] contains the full path of the
            directory where the script is located. See <http://docs.python.org/
            lib/module-sys.htmlunder 'path'.
            >
            it should be straightforward from here (untested though). In each
            script, get the sys.path[0] string, split it using os.path, replace
            the last item with 'tools' and join again with os.path. If the
            resulting string is not in sys.path, insert it after sys.path[0].
            >
            /Jean Brouwers
            >
            On Apr 1, 1:57 pm, gamename <namesagame-use...@yahoo.co mwrote:
            >
            Hi,
            >
            I generally have several copies of the same development environment
            checked out from cvs at any one time. Each development tree has a
            'tools' dir containing python modules. Scattered in different places
            in the tree are various python scripts.
            >
            What I want to do is force my scripts to always look in the closest
            'tools' dir for any custom modules to import. For example:
            >
            tree1/tools
            tree1/my_scripts/foo.py
            >
            tree2/tools
            tree2/my_scripts/foo.py
            >
            How can I make 'foo.py' always look in '../../tools' for custom
            modules? My preference would be to avoid changing the 'foo.py' script
            and have some way to resolve it via the environment (like PYTHONPATH,
            or .pth files, etc.).
            >
            Anyone have any ideas?
            TIA,
            -T

            Comment

            • gamename

              #7
              Re: Not Sure This Can Be Done...

              Hi,

              In hopes it may help someone else, here's what I ended up doing:

              1) Add this to to point to your local 'tools' directory:
              import site
              # the python tools dir is at "../../tools/python"
              site.addsitedir ('..'+os.sep+'. .'+os.sep+'tool s'+os.sep
              +'python')

              2) In the 'tools' dir, there is a file called 'local.pth'. Its
              contents:
              # the individual modules are at "../../tools/python/modules/
              <modulename>"
              ./modules/pexpect

              3) In the script you can now do:
              import pexpect

              That's it. This works fine, but there are still 2 outstanding issues:

              A) How do make the same ".pth" file usable on a win32 environment?
              The path separators are different (i.e. "/" vs "\").

              B) How to auto-include any subdirectory in 'python/modules' so that
              each
              new module dir doesn't have to be added manually? That is, if
              package
              'foo' is added, it would be in 'python/modules/foo'. Is there a
              way
              to avoid manually putting that in the '.pth'?

              FYI,
              -T

              Comment

              Working...