specify arbitrary library directory directly in code?

Discussion in 'Python' started by scooterm@hotmail.com, Oct 28, 2005.

  1. Guest

    ### Background
    In [other programming language] it is possible to specify any special
    and arbitrary directory (or set of directories) to use as a source
    for custom, user-designed modules.

    For example, one can do:
    ### begin script
    ### (located in ../faa/faa_script.xyz)

    ### init script
    use lib "../foo";
    use FooLibModule;

    use lib "../fee";
    use FeeLibModule;

    ### run script
    print FooLibModule::SayHelloWorld();
    print FeeLibModule::SayHelloWorld();

    ### Initial Findings
    After searching around a bit, it appears that python does not have a
    built-in mechanism for specifying arbitrary directories as the first
    place to look for module code. There are options that allow one to
    specify alternate directories, but all these options require changes
    to environment variables, or changes (or additions) to the files in
    the standard package directories.

    But if you define 'built-in mechanism' meaning a change that only
    requires modifying the code in 'faa_script.py' ... then there is
    no 'built-in mechanism'.

    ### Question
    Are the initial findings correct? If not, can you specify the code
    (or the link to the documentation) that specifies how to do what I
    am trying to do? If the initial findings are correct, are there any
    alternative workarounds to allow faa_script.py to automatically
    discover module code in specific sibling directories?

    ### Constraints
    Assume changing or adding environment variables is not an option.
    Assume adding files to the standard python module directories is not an

    option.
    Assume the only option is to change the code in faa_script.py
    , Oct 28, 2005
    #1
    1. Advertising

  2. Mike Meyer Guest

    writes:

    > ### Initial Findings
    > After searching around a bit, it appears that python does not have a
    > built-in mechanism for specifying arbitrary directories as the first
    > place to look for module code. There are options that allow one to
    > specify alternate directories, but all these options require changes
    > to environment variables, or changes (or additions) to the files in
    > the standard package directories.
    >
    > But if you define 'built-in mechanism' meaning a change that only
    > requires modifying the code in 'faa_script.py' ... then there is
    > no 'built-in mechanism'.
    >
    > ### Question
    > Are the initial findings correct? If not, can you specify the code
    > (or the link to the documentation) that specifies how to do what I
    > am trying to do? If the initial findings are correct, are there any
    > alternative workarounds to allow faa_script.py to automatically
    > discover module code in specific sibling directories?


    No, the findings aren't correct. You can manipulate the search path
    directly.

    import sys
    sys.path.insert(0, "/directory/to/search")

    will cause future imports to search /directory/to/search before all
    other directories. Likewise, sys.append("/directory/to/search") will
    search it after other directories.

    Docs here: http://www.python.org/doc/2.2.3/tut/node8.html#SECTION008200000000000000000

    <mike
    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
    Mike Meyer, Oct 28, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Till Soerensen
    Replies:
    1
    Views:
    1,226
    Henry S. Thompson
    Jun 9, 2004
  2. Honestmath
    Replies:
    5
    Views:
    557
    Honestmath
    Dec 13, 2004
  3. Russell Warren
    Replies:
    4
    Views:
    493
    Stefan Behnel
    Jul 13, 2006
  4. Replies:
    14
    Views:
    622
    roy axenov
    Oct 8, 2006
  5. Simon Brooke
    Replies:
    10
    Views:
    1,873
    Simon Brooke
    Jul 23, 2010
Loading...

Share This Page