hpw to convert a linux python script ?

Discussion in 'Python' started by Stef Mientki, Feb 11, 2009.

  1. Stef Mientki

    Stef Mientki Guest

    hello,

    I've a python script, written for some Linux version,
    now I want to run it under windows.
    It complains of not finding files in
    /usr/share/tinybldLin/ ....
    where .... is the directory where the script is located and started from.

    As there are a whole lot of these lines, in a whole lot of files,
    I wonder if there's a simple trick to point
    /usr/share/tinybldLin/
    to my directory ?


    thanks,
    Stef
    Stef Mientki, Feb 11, 2009
    #1
    1. Advertising

  2. On Feb 11, 7:58 pm, Stef Mientki <> wrote:
    > As there are a whole lot of these lines, in a whole lot of files,
    > I wonder if there's a simple trick to point
    >   /usr/share/tinybldLin/
    > to my directory ?
    >
    > thanks,
    > Stef


    Find and replace?
    Alec Schueler, Feb 11, 2009
    #2
    1. Advertising

  3. Stef Mientki

    Stef Mientki Guest

    Alec Schueler wrote:
    > On Feb 11, 7:58 pm, Stef Mientki <> wrote:
    >
    >> As there are a whole lot of these lines, in a whole lot of files,
    >> I wonder if there's a simple trick to point
    >> /usr/share/tinybldLin/
    >> to my directory ?
    >>
    >> thanks,
    >> Stef
    >>

    >
    > Find and replace?
    >

    well I was thinking of a more elegant way,
    so the scripts remains, or better become multi-platform.

    anyway thanks,
    Stef
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
    Stef Mientki, Feb 11, 2009
    #3
  4. Stef Mientki

    Guest

    Stef Mientki <> wrote:
    > Alec Schueler wrote:
    > > On Feb 11, 7:58 pm, Stef Mientki <> wrote:
    > >
    > >> As there are a whole lot of these lines, in a whole lot of files,
    > >> I wonder if there's a simple trick to point
    > >> /usr/share/tinybldLin/
    > >> to my directory ?
    > >>
    > >> thanks,
    > >> Stef
    > >>

    > >
    > > Find and replace?
    > >

    > well I was thinking of a more elegant way,
    > so the scripts remains, or better become multi-platform.
    >
    > anyway thanks,


    Well, you'll have to rewrite the script so that it doesn't make
    assumptions about path names, and we can't help you with that without
    seeing some of the script code. There _might_ be easy fixes, but if the
    path is hardcoded in many places in the script...then you will just have
    to go through and un-hard-code it.

    --RDM
    , Feb 11, 2009
    #4
  5. Stef Mientki

    John Machin Guest

    On Feb 12, 7:48 am, Stef Mientki <> wrote:
    > Alec Schueler wrote:
    > > On Feb 11, 7:58 pm, Stef Mientki <> wrote:

    >
    > >> As there are a whole lot of these lines, in a whole lot of files,
    > >> I wonder if there's a simple trick to point
    > >>   /usr/share/tinybldLin/
    > >> to my directory ?

    >
    > >> thanks,
    > >> Stef

    >
    > > Find and replace?

    >
    > well I was thinking of a more elegant way,
    > so the scripts remains, or better become multi-platform.


    Your current setup is not even single-platform portable ... consider
    the scenario where some Linux admin wants your gear to live in some
    directory other than /usr/share/tinybldLin/

    The standard technique for ensuring that kind of portability involves:
    1. Find the directory in which your script lives. Inspect sys.argv[0],
    use some os.path.some_functionality() to extract the directory, name
    it (say) home_base.
    2. When you want to open a data file in the script's directory, do
    this:
    the_path = os.path.join(home_base, 'stef1.dat')
    f = open(the_path, .....)

    NOTE: Use os.path.join() instead of some DIY technique; it knows
    better than you what the path separator is on the OS it's being run
    on. In fact, read the whole of the os.path manual carefully before you
    jump in to changing your code.

    If you prefer to have the data files in (say) .../script_locn/data
    instead of .../script_locn, the adjustment should be obvious.

    Unfortunately, you are still going to need use find and replace to fix
    your script. Code in haste, repent at leisure :)

    HTH,
    John
    John Machin, Feb 11, 2009
    #5
  6. Stef Mientki

    Steve Holden Guest

    John Machin wrote:
    > On Feb 12, 7:48 am, Stef Mientki <> wrote:
    >> Alec Schueler wrote:
    >>> On Feb 11, 7:58 pm, Stef Mientki <> wrote:
    >>>> As there are a whole lot of these lines, in a whole lot of files,
    >>>> I wonder if there's a simple trick to point
    >>>> /usr/share/tinybldLin/
    >>>> to my directory ?
    >>>> thanks,
    >>>> Stef
    >>> Find and replace?

    >> well I was thinking of a more elegant way,
    >> so the scripts remains, or better become multi-platform.

    >
    > Your current setup is not even single-platform portable ... consider
    > the scenario where some Linux admin wants your gear to live in some
    > directory other than /usr/share/tinybldLin/
    >
    > The standard technique for ensuring that kind of portability involves:
    > 1. Find the directory in which your script lives. Inspect sys.argv[0],
    > use some os.path.some_functionality() to extract the directory, name
    > it (say) home_base.
    > 2. When you want to open a data file in the script's directory, do
    > this:
    > the_path = os.path.join(home_base, 'stef1.dat')
    > f = open(the_path, .....)
    >
    > NOTE: Use os.path.join() instead of some DIY technique; it knows
    > better than you what the path separator is on the OS it's being run
    > on. In fact, read the whole of the os.path manual carefully before you
    > jump in to changing your code.
    >
    > If you prefer to have the data files in (say) .../script_locn/data
    > instead of .../script_locn, the adjustment should be obvious.
    >
    > Unfortunately, you are still going to need use find and replace to fix
    > your script. Code in haste, repent at leisure :)
    >

    Of course the real issue here is that the same hard-coded directory name
    (or possibly multiple directory names all rooted in the same directory)
    are scattered throughout the program. Your (the OP's) first task should
    be to store that "root" directory in a variable and then use the
    variable in place of all current uses of the root directory.

    For example, I have a Django project that I like to run under both
    Windows and Cygwin. So in my settings.py file I have:

    # Django settings for PyTeach project.
    import sys
    if sys.platform == 'cygwin':
    fsroot = "/home/sholden"
    elif sys.platform == 'win32':
    fsroot = "C:/Users/sholden/Documents"
    else:
    sys.exit("Only configured for Windows and Cygwin")

    Than further down I have

    # Absolute path to the directory that holds media.
    # Example: "/home/media/media.lawrence.com/"
    MEDIA_ROOT = fsroot+'/Projects/PytDj/images/'

    Note that most Windows APIs allow you to use the forward slash as a
    delimiter. It's mostly the command line and Windows Explorer that are
    snotty about insisting on the backslash.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
    Steve Holden, Feb 12, 2009
    #6
  7. > # Absolute path to the directory that holds media.
    > # Example: "/home/media/media.lawrence.com/"
    > MEDIA_ROOT = fsroot+'/Projects/PytDj/images/'
    >
    > Note that most Windows APIs allow you to use the forward slash as a
    > delimiter. It's mostly the command line and Windows Explorer that are
    > snotty about insisting on the backslash.


    Idle curiosity, why do you prefer that to:

    MEDIA_ROOT = os.path.join(fsroot, 'Projects', 'PytDj', 'images')

    Not arguing that one is actually better then the other, just curious.

    In my own environment we have grown a whole big filesystem abstraction
    layer because we have to be able to address the same resources on
    network volumes in the same client on multiple OS's (mainly windows
    and mac presently, but a linux client has long been a
    consideration),... and on the mac having to sometimes use mac paths
    (":this:that:eek:ther") and sometimes posix paths ("/this/that/other")
    depending on what API layer I'm poking at in a given point of view...
    so personally I just end up being mildly curious how everyone else
    handles paths and such.

    Cuz it was quite a bit of a pain early on.

    --S
    Stephen Hansen, Feb 13, 2009
    #7
  8. Stef Mientki

    Steve Holden Guest

    Stephen Hansen wrote:
    >> # Absolute path to the directory that holds media.
    >> # Example: "/home/media/media.lawrence.com/"
    >> MEDIA_ROOT = fsroot+'/Projects/PytDj/images/'
    >>
    >> Note that most Windows APIs allow you to use the forward slash as a
    >> delimiter. It's mostly the command line and Windows Explorer that are
    >> snotty about insisting on the backslash.

    >
    > Idle curiosity, why do you prefer that to:
    >
    > MEDIA_ROOT = os.path.join(fsroot, 'Projects', 'PytDj', 'images')
    >
    > Not arguing that one is actually better then the other, just curious.
    >

    Going for "good enough", I suppose. It's more readable, at least, not
    that that makes a *huge* difference.

    > In my own environment we have grown a whole big filesystem abstraction
    > layer because we have to be able to address the same resources on
    > network volumes in the same client on multiple OS's (mainly windows
    > and mac presently, but a linux client has long been a
    > consideration),... and on the mac having to sometimes use mac paths
    > (":this:that:eek:ther") and sometimes posix paths ("/this/that/other")
    > depending on what API layer I'm poking at in a given point of view...
    > so personally I just end up being mildly curious how everyone else
    > handles paths and such.
    >
    > Cuz it was quite a bit of a pain early on.


    The old Mac paths would indeed require your solution, which is strictly
    speaking the best.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
    Steve Holden, Feb 15, 2009
    #8
    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. Anton Vredegoor

    Hpw make lists that are easy to sort.

    Anton Vredegoor, Mar 28, 2007, in forum: Python
    Replies:
    10
    Views:
    476
    Anton Vredegoor
    Mar 31, 2007
  2. Gros Bedo
    Replies:
    2
    Views:
    1,292
    Piet van Oostrum
    Jul 10, 2008
  3. Gros Bedo
    Replies:
    1
    Views:
    660
    Piet van Oostrum
    Jul 10, 2008
  4. norseman
    Replies:
    0
    Views:
    561
    norseman
    Jul 10, 2008
  5. Gros Bedo
    Replies:
    0
    Views:
    420
    Gros Bedo
    Jul 13, 2008
Loading...

Share This Page