Appending to sys.path

Discussion in 'Python' started by mhearne808[insert-at-sign-here]gmail[insert-dot-he, Mar 24, 2009.

  1. I have an application where I would like to append to the python path
    dynamically. Below is a test script I wrote. Here's what I thought
    would happen:

    1) I run this script in a folder that is NOT already in PYTHONPATH
    2) The script creates a subfolder called foo.
    3) The script creates a file called foo.py, with a foo() method
    defined in it.
    4) The script attempts to import this module, which fails because the
    current folder is not in PYTHONPATH.
    5) The script then adds the current folder to the end of sys.path
    6) The script again attempts to import foo.foo, and succeeds.

    #6 never happens, but I can't figure out why. I'm running on Mac OS
    10.5, with python 2.5.1.

    Anybody have any tips?

    Thanks,

    Mike

    Here's the script:
    #!/usr/bin/python

    import sys
    import os.path

    txt = 'def foo(): print "Hello world!"\n'
    if not os.path.isdir('foo'):
    os.mkdir('foo')
    f = open('foo/foo.py','wt')
    f.write(txt)
    f.close()

    try:
    __import__('foo.foo')
    except ImportError:
    homedir = os.path.abspath(sys.path[0]) #where is this script?
    print 'Adding %s to sys.path' % (homedir)
    sys.path.append(homedir)
    try:
    __import__('foo.foo')
    print 'Import now successful'
    except ImportError:
    print "Why didn't this work?"
    sys.exit(1)
    mhearne808[insert-at-sign-here]gmail[insert-dot-he, Mar 24, 2009
    #1
    1. Advertising

  2. mhearne808[insert-at-sign-here]gmail[insert-dot-he

    Peter Otten Guest

    mhearne808[insert-at-sign-here]gmail[insert-dot-here]com wrote:

    > I have an application where I would like to append to the python path
    > dynamically. Below is a test script I wrote. Here's what I thought
    > would happen:
    >
    > 1) I run this script in a folder that is NOT already in PYTHONPATH
    > 2) The script creates a subfolder called foo.
    > 3) The script creates a file called foo.py, with a foo() method
    > defined in it.
    > 4) The script attempts to import this module, which fails because the
    > current folder is not in PYTHONPATH.
    > 5) The script then adds the current folder to the end of sys.path
    > 6) The script again attempts to import foo.foo, and succeeds.
    >
    > #6 never happens, but I can't figure out why. I'm running on Mac OS
    > 10.5, with python 2.5.1.
    >
    > Anybody have any tips?
    >
    > Thanks,
    >
    > Mike
    >
    > Here's the script:
    > #!/usr/bin/python
    >
    > import sys
    > import os.path
    >
    > txt = 'def foo(): print "Hello world!"\n'
    > if not os.path.isdir('foo'):


    Here you add a directory under the current working directory.

    > os.mkdir('foo')
    > f = open('foo/foo.py','wt')
    > f.write(txt)
    > f.close()


    To turn the foo directory into a package you must also create a file
    foo/__init__.py (may be empty).
    >
    > try:
    > __import__('foo.foo')
    > except ImportError:
    > homedir = os.path.abspath(sys.path[0]) #where is this script?
    > print 'Adding %s to sys.path' % (homedir)
    > sys.path.append(homedir)


    Here you add the directory to the path were the script is located, not the
    cwd. Should be

    sys.path.append(".")

    > try:
    > __import__('foo.foo')
    > print 'Import now successful'
    > except ImportError:
    > print "Why didn't this work?"
    > sys.exit(1)


    Peter
    Peter Otten, Mar 24, 2009
    #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. Jeff Wagner

    sys.path.append('/my/new/path')

    Jeff Wagner, Nov 24, 2003, in forum: Python
    Replies:
    3
    Views:
    526
    Patrick Useldinger
    Nov 24, 2003
  2. Replies:
    3
    Views:
    398
  3. wen
    Replies:
    10
    Views:
    970
    Peter Hansen
    Aug 19, 2005
  4. Greg McIntyre
    Replies:
    0
    Views:
    431
    Greg McIntyre
    Sep 12, 2005
  5. Darren Hart
    Replies:
    0
    Views:
    210
    Darren Hart
    Oct 30, 2011
Loading...

Share This Page