FW: python import sys.path

Discussion in 'Python' started by Kelly, Brian, Jan 2, 2009.

  1. Kelly, Brian

    Kelly, Brian Guest

    After following your suggestions I was able to confirm that the 2.5
    interpreter was being invoked. So then I grepped for all instances of python
    in the scripts that were imported as modules: from bacula_conf import *


    The calling script cleanup.py is invoking purge_client.py like an external
    script:

    def purgeAll(options, computer_name):
    cmd = "python purge_client.py %s" % computer_name
    if options.pretend <> True:
    error = os.system(cmd)
    else:
    _log.info("Done. No changes made due to --pretend flag.")
    if not error:
    return True
    else:
    return False


    When I saw the imports I assumed the functions in purge_client.py were being
    referenced from the calling scripts symbol table. However, that is not the
    case.

    Thanks,
    Brian
    -----Original Message-----
    From: John Machin [mailto:]
    Sent: Tuesday, December 30, 2008 4:42 PM
    To:
    Subject: Re: python import sys.path

    On Dec 31, 5:05 am, "Kelly, Brian" <> wrote:
    > I have both 2.4 and 2.5 interpreters installed on a linux box. The
    > PythonPath is set to :
    >
    >

    PYTHONPATH=/usr/lib64/portage/pym:/prod/bacula/local/lib64/python2.4/site-pa
    > ckages:/prod/bacula/local/lib/python2.4/site-packages
    >
    > My main script is getting called like so:
    >
    > python2.4 cleanup.py wrkstnbs
    >
    > The imports statements in cleanup.py are as follows:
    >
    > import os,sys
    > print sys.path
    > from datetime import datetime
    > from optparse import OptionParser        # used for parsing parameters
    > from bacula_conf import *                # used for connecting to our
    > databases, etc.
    > from registration_cleanup \
    >     import RegistrationCleanup           # used for interacting w/
    > registration db (sql1)
    >                                          # and configuration database
    > (genunix)
    > import directory_cleanup as fclean       # file cleanup.
    >
    > One of the scripts being imported from bacula_conf is called
    > purge_client.py.
    >
    > It has the following imports:
    >
    > import sys
    > import MySQLdb
    >
    > Everytime I run "python2.4 cleanup.py wrkstnbs" I get the following error:
    >
    > Traceback (most recent call last):


    Have you snipped any traceback entries here? You say you are running
    cleanup.py but the first traceback entry is from purge_client.py!!

    You should first fix that, so that you've got the full story. My guess
    is that something along that trail is invoking another instance of the
    Python interpreter and somehow that instance is 2.5, not 2.4. I can't
    imagine how those 2.5-related entries would otherwise get into
    sys.path

    Suggestions: (1) where you are debug-printing sys.path, also print
    sys.version and sys.argv[0] ... and do debug-printing at more places
    between start and error.
    (2) run "python2.4 -vv cleanup.py wrkstnbs" and look for the first
    mention of 2.5 (indicating something has manipulated sys.path), or
    sudden cessation of -vv output (indicating a new instance of python is
    running without -vv), or some other phenomenon ...

    >   File "purge_client.py", line 22, in <module>
    >     import MySQLdb
    >   File
    > "/prod/bacula/local/lib64/python2.4/site-packages/MySQLdb/__init__.py",

    line
    > 27, in <module>
    >     import _mysql
    > ImportError: /prod/bacula/local/lib64/python2.4/site-packages/_mysql.so:
    > undefined symbol: Py_InitModule4


    What happens when you run python2.4 and at the interactive prompt do
    >>> import _mysql

    ? Is there a possibility that you installed 2.5 MySQLdb into the 2.4
    hierarchy? [could be a stupid question from a windows guy; this may be
    a non-problem on linux]

    Is this the first thing that you've tried after installing 2.5, or the
    only problem found in an exhaustive regression test of all your apps
    using 2.4, or somewhere in the middle?

    [big snip]

    HTH,
    John
     
    Kelly, Brian, Jan 2, 2009
    #1
    1. Advertising

  2. Kelly, Brian

    John Machin Guest

    On Jan 3, 1:09 am, "Kelly, Brian" <> wrote:
    > After following your suggestions I was able to confirm that the 2.5
    > interpreter was being invoked. So then I grepped for all instances of python
    > in the scripts that were imported as modules: from bacula_conf import *
    >
    > The calling script cleanup.py is invoking purge_client.py like an external
    > script:
    >
    > def purgeAll(options, computer_name):
    >     cmd = "python purge_client.py %s" % computer_name
    >     if options.pretend <> True:

    Who wrote that?


    >         error = os.system(cmd)
    >     else:
    >         _log.info("Done. No changes made due to --pretend flag.")

    #### error is not defined in this branch
    >     if not error: #### Splat!
    >         return True
    >     else:
    >         return False

    AArrgh! Try:

    return not error

    > When I saw the imports I assumed the functions in purge_client.py were being
    > referenced from the calling scripts symbol table.


    What does that mean??
     
    John Machin, Jan 2, 2009
    #2
    1. Advertising

  3. Kelly, Brian

    Fuzzyman Guest

    On Jan 2, 2:28 pm, John Machin <> wrote:
    > On Jan 3, 1:09 am, "Kelly, Brian" <> wrote:> After following your suggestions I was able to confirm that the 2.5
    > > interpreter was being invoked. So then I grepped for all instances of python
    > > in the scripts that were imported as modules: from bacula_conf import *

    >
    > > The calling script cleanup.py is invoking purge_client.py like an external
    > > script:

    >
    > > def purgeAll(options, computer_name):
    > >     cmd = "python purge_client.py %s" % computer_name
    > >     if options.pretend <> True:

    >
    > Who wrote that?
    >
    > >         error = os.system(cmd)
    > >     else:
    > >         _log.info("Done. No changes made due to --pretend flag.")

    >
    >            #### error is not defined in this branch>     if not error: #### Splat!
    > >         return True
    > >     else:
    > >         return False

    >
    >  AArrgh! Try:
    >
    >    return not error
    >


    That will still blow up with a NameError when the path doing the
    logging is invoked.

    How about:


    def purgeAll(options, computer_name):
    cmd = "python purge_client.py %s" % computer_name
    if options.pretend != True: # if not options.pretend (assuming it
    is a bool)
    error = os.system(cmd)
    return not error

    _log.info("Done. No changes made due to --pretend flag.")
    return True


    Michael
    --
    http://www.ironpythoninaction.com/
     
    Fuzzyman, Jan 2, 2009
    #3
    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. flupke
    Replies:
    2
    Views:
    1,275
    flupke
    Sep 26, 2005
  2. Frank Millman

    Question about import and sys.path

    Frank Millman, Nov 28, 2006, in forum: Python
    Replies:
    3
    Views:
    327
    Rob Wolfe
    Nov 29, 2006
  3. Jeremy Nicoll - news posts

    Import, how to change sys.path on Windows, and module naming?

    Jeremy Nicoll - news posts, Mar 1, 2008, in forum: Python
    Replies:
    7
    Views:
    698
    Gabriel Genellina
    Mar 4, 2008
  4. Replies:
    1
    Views:
    323
  5. Kelly, Brian

    python import sys.path

    Kelly, Brian, Dec 30, 2008, in forum: Python
    Replies:
    1
    Views:
    2,291
    John Machin
    Dec 30, 2008
Loading...

Share This Page