Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Appending to sys.path

Reply
Thread Tools

Appending to sys.path

 
 
mhearne808[insert-at-sign-here]gmail[insert-dot-here]com
Guest
Posts: n/a
 
      03-24-2009
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)
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      03-24-2009
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Appending to a dropdown list Maziar Aflatoun ASP .Net 4 09-23-2004 01:45 PM
Appending a DataSet Philip Townsend ASP .Net 1 12-04-2003 03:33 PM
Prevent Netscape from appending .aspx during FileSave? Steve E. ASP .Net 0 11-11-2003 07:18 PM
Netscape Appending .aspx to downloads Jeff Cooper ASP .Net 0 10-21-2003 12:24 PM
Appending to XML Andrew Cholakian Perl 0 07-17-2003 08:27 PM



Advertisments