More elegant way to cwd?

Discussion in 'Python' started by Kamilche, Dec 24, 2004.

  1. Kamilche

    Kamilche Guest

    Is there a more elegant way to change the working directory of Python
    to the directory of the currently executing script, and add a folder
    called 'Shared' to the Python search path?

    This is what I have. It seems like it could be shorter, somehow.

    # Switch Python to the current directory
    import os, sys
    pathname, scriptname = os.path.split(sys.argv[0])
    pathname = os.path.abspath(pathname)
    os.chdir(pathname)
    s = os.path.realpath('..')
    s = os.path.join(s, 'Shared')
    sys.path.append(s)

    Thanks for any enhancements you can suggest!
    Kamilche, Dec 24, 2004
    #1
    1. Advertising

  2. Kamilche

    M.E.Farmer Guest

    Hello,
    Seems you found os.chdir(), but you missed os.getcwd()
    ;)
    M.E.Farmer
    M.E.Farmer, Dec 24, 2004
    #2
    1. Advertising

  3. Kamilche

    M.E.Farmer Guest

    Ignore my last post to much eggnog I guess. I must have parsed it to
    quickly.

    M.E.Farmer
    M.E.Farmer, Dec 24, 2004
    #3
  4. Kamilche

    Peter Hansen Guest

    Kamilche wrote:
    > Is there a more elegant way to change the working directory of Python


    That depends on how you define "elegant", I guess.

    > to the directory of the currently executing script, and add a folder
    > called 'Shared' to the Python search path?
    >
    > This is what I have. It seems like it could be shorter, somehow.


    It could be shorter if you were willing to combine several
    function calls on the same line, but I get the impression
    you wouldn't consider that more elegant...

    > # Switch Python to the current directory
    > import os, sys
    > pathname, scriptname = os.path.split(sys.argv[0])
    > pathname = os.path.abspath(pathname)
    > os.chdir(pathname)
    > s = os.path.realpath('..')
    > s = os.path.join(s, 'Shared')
    > sys.path.append(s)
    >
    > Thanks for any enhancements you can suggest!


    Other than using os.pardir instead of '..', and possibly adding
    an "os.path.abspath()" call to the last bit (or does realpath
    already do that? It's unclear from the docs), I can't see
    anything fundamental I'd do differently... except package these
    functions up as nice clean subroutines, possibly in a library
    package, that I could then use in code that would magically
    become "elegant" (IMHO) by avoiding the repetition of all
    that non-elegant stuff above...

    -Peter
    Peter Hansen, Dec 27, 2004
    #4
  5. Kamilche

    F. Petitjean Guest

    On Mon, 27 Dec 2004 11:53:57 -0500, Peter Hansen <> wrote:
    > Kamilche wrote:
    >> Is there a more elegant way to change the working directory of Python

    >
    > That depends on how you define "elegant", I guess.
    >
    >> to the directory of the currently executing script, and add a folder
    >> called 'Shared' to the Python search path?
    >>
    >> This is what I have. It seems like it could be shorter, somehow.

    >
    > It could be shorter if you were willing to combine several
    > function calls on the same line, but I get the impression
    > you wouldn't consider that more elegant...
    >
    >> # Switch Python to the current directory
    >> import os, sys
    >> pathname, scriptname = os.path.split(sys.argv[0])
    >> pathname = os.path.abspath(pathname)
    >> os.chdir(pathname)
    >> s = os.path.realpath('..')
    >> s = os.path.join(s, 'Shared')
    >> sys.path.append(s)
    >>
    >> Thanks for any enhancements you can suggest!

    >

    How about ?
    import os, sys
    pathname, scriptname = os.path.split(sys.argv[0])
    pathname = os.path.abspath(pathname)
    os.chdir(pathname) # only if you really need to cwd
    parent = os.path.dirname(pathname)
    parent = os.path.join(parent, 'Shared')
    if parent not in sys.path:
    sys.path.append(parent)


    Or even :
    pathname, scriptname = os.path.split(__file__)
    # pathname should be an absolute path
    F. Petitjean, Dec 27, 2004
    #5
  6. Kamilche

    M.E.Farmer Guest

    Peter Hansen wrote:
    [snip]
    > Other than using os.pardir instead of '..', and possibly adding
    > an "os.path.abspath()" call to the last bit (or does realpath
    > already do that? It's unclear from the docs)

    [snip]
    I believe os.path.abspath and os.path.realpath are the same.
    realpath is just an alias for abspath.
    Using pydoc on os gives this:

    py>realpath = abspath(path)
    py> Return the absolute version of a path

    M.E.Farmer
    M.E.Farmer, Dec 28, 2004
    #6
  7. Kamilche

    Kamilche Guest

    > Other than using os.pardir instead of '..', and possibly adding
    > an "os.path.abspath()" call to the last bit (or does realpath
    > already do that? It's unclear from the docs), I can't see
    > anything fundamental I'd do differently... except package these
    > functions up as nice clean subroutines, possibly in a library
    > package, that I could then use in code that would magically
    > become "elegant" (IMHO) by avoiding the repetition of all
    > that non-elegant stuff above...
    >
    > -Peter


    Well... but to call it from the shared directory, I'd have to first
    switch to the shared directory! Which would defeat the purpose.

    I wish there was a way to store this in a single file and call it from
    any script, but I'm unwilling to keep all my scripts in one folder to
    accomodate it. If only Python would let you import modules from
    different directories. :-/

    --Kamilche
    Kamilche, Dec 28, 2004
    #7
  8. Kamilche

    Steve Holden Guest

    Kamilche wrote:

    >> Other than using os.pardir instead of '..', and possibly adding
    >> an "os.path.abspath()" call to the last bit (or does realpath
    >> already do that? It's unclear from the docs), I can't see
    >> anything fundamental I'd do differently... except package these
    >> functions up as nice clean subroutines, possibly in a library
    >> package, that I could then use in code that would magically
    >> become "elegant" (IMHO) by avoiding the repetition of all
    >> that non-elegant stuff above...
    >>
    >> -Peter

    >
    >
    > Well... but to call it from the shared directory, I'd have to first
    > switch to the shared directory! Which would defeat the purpose.
    >
    > I wish there was a way to store this in a single file and call it from
    > any script, but I'm unwilling to keep all my scripts in one folder to
    > accomodate it. If only Python would let you import modules from
    > different directories. :-/
    >
    > --Kamilche


    Maybe I'm misunderstanding your requirements, but it seems to me
    perfectly practical to keep that script in a standard place (say,
    lib/site-packages, which was intended for exactly that purpose) and
    import it from there. There's no reason why its location has to relate
    to the location of the scripts that import it and use that function from it.

    What am I missing?

    regards
    Steve
    --
    Steve Holden http://www.holdenweb.com/
    Python Web Programming http://pydish.holdenweb.com/
    Holden Web LLC +1 703 861 4237 +1 800 494 3119
    Steve Holden, Dec 28, 2004
    #8
  9. Kamilche

    Georg Brandl Guest

    M.E.Farmer wrote:
    > Peter Hansen wrote:
    > [snip]
    >> Other than using os.pardir instead of '..', and possibly adding
    >> an "os.path.abspath()" call to the last bit (or does realpath
    >> already do that? It's unclear from the docs)

    > [snip]
    > I believe os.path.abspath and os.path.realpath are the same.
    > realpath is just an alias for abspath.


    Yes, on Windows. On UNIX systems, realpath resolves symbolic links while
    abspath doesn't.

    mfg
    Georg
    Georg Brandl, Dec 28, 2004
    #9
  10. Kamilche

    Peter Hansen Guest

    Kamilche wrote:
    > Well... but to call it from the shared directory, I'd have to first
    > switch to the shared directory! Which would defeat the purpose.


    As Steve said, plus "use PYTHONPATH or .pth files". That's
    exactly what they're for.

    Doing what you're doing as a means of getting access to
    regularly used shared modules makes no sense. Within an
    application, to get access to a particular, special set of
    modules for some reason, that can often make sense. For
    "library" modules there are already other and more elegant
    means.

    -Peter
    Peter Hansen, Dec 28, 2004
    #10
  11. Kamilche

    Kamilche Guest

    > For "library" modules there are already other and more elegant means.
    >
    > -Peter


    Well, I want to make a script execute without error regardless of where
    the current working directory is, and without having to make the user
    modify their PYTHONPATH variable or install something in site-packages.

    Is there a way to let the user just 'drag the folder' anywhere on their
    hard drive, and have it work, without having to modify any special
    system variables?
    Kamilche, Dec 29, 2004
    #11
  12. Kamilche

    Peter Hansen Guest

    Kamilche wrote:
    >> For "library" modules there are already other and more elegant means.

    >
    > Well, I want to make a script execute without error regardless of where
    > the current working directory is, and without having to make the user
    > modify their PYTHONPATH variable or install something in site-packages.


    If you are talking about just "a script", then this of course
    works out of the box without doing anything special. You just
    run the script and it works.

    If, on the other hand, the script requires access to certain
    shared *other* modules, then no, if you don't tell the script
    where to find those other modules, and they are not in the same
    folder as the script itself, then you can't do this. You do
    have to tell the Python where to find things... I'm unclear
    what your situation is, that you have shared modules that you
    want to use in various other scripts, and yet you have some
    strange restriction preventing you from telling Python where
    those are to be found except with custom code written in each
    script that will use the modules.

    Even if that really describes your (very bizarre, IMHO) situation,
    the least you could do is write the aforementioned code in a
    nice subroutine that you just cut and paste into each module,
    then call "elegantly". It's code reuse, even if it's the most
    rudimentary sort of reuse.

    > Is there a way to let the user just 'drag the folder' anywhere on their
    > hard drive, and have it work, without having to modify any special
    > system variables?


    Not sure exactly what you are looking for. I certain have folders
    of Python scripts that I can just drag here or there and have them
    work. When they need to use common library files, however, I take
    a moment to set that up ahead of time, using .pth files if I can't
    or won't install them in site-packages.

    -Peter
    Peter Hansen, Dec 29, 2004
    #12
    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. mikegw
    Replies:
    5
    Views:
    369
    Nick Keighley
    May 24, 2004
  2. Kamilche
    Replies:
    7
    Views:
    282
    Peter Hansen
    Jun 29, 2004
  3. Replies:
    5
    Views:
    376
  4. Lilith
    Replies:
    8
    Views:
    348
    Dave Rahardja
    Sep 2, 2005
  5. Gilles Ganault
    Replies:
    9
    Views:
    260
    Boris Borcic
    Nov 20, 2008
Loading...

Share This Page