[wxPython] The Current Working Directory and Packages(Modules)

I'm using the wxFileDialog which allows the user to change the current
working directory. Once this happens my code no longer knows where all of
it's modules are. What do I need to do in order to keep my code linked
together when the current working directory keeps changing?

  -Mikhael

I'm using the wxFileDialog which allows the user to change the current
working directory. Once this happens my code no longer knows where all of
it's modules are. What do I need to do in order to keep my code linked
together when the current working directory keeps changing?

The directory where the main module of your app (the .py file that python
executes at startup) is located should be automatically placed on the
sys.path, so any modules or packages located in that directory should be
found no matter what the CWD is. For example, in c:\temp I have testpath.py
with this content:

import sys, pprint
print
pprint.pprint(sys.path)

Here it is run from a different directory:

[C:\] c:\temp\testpath.py

['C:\\temp',
'..',
'.',
'c:\\tools\\python20\\pythonwin',
'c:\\tools\\python20\\win32',
'c:\\tools\\python20\\win32\\lib',
'c:\\tools\\python20',
'c:\\tools\\python20\\dlls',
'c:\\tools\\python20\\lib',
'c:\\tools\\python20\\lib\\plat-win',
'c:\\tools\\python20\\lib\\lib-tk',
'c:\\tools\\python20\\numeric',
'c:\\tools\\python20\\pil',
'c:\\PyLib']

[C:\]

If you have modules/packages located elsewhere then you can either add them
to the PYTHONPATH environment variable before your app runs, or add them to
sys.path during startup.

···

--
Robin Dunn
Software Craftsman
robin@AllDunn.com Java give you jitters?
http://wxPython.org Relax with wxPython!

Hi Michael,

I'm using the wxFileDialog which allows the user to change the current
working directory. Once this happens my code no longer knows where all of
it's modules are. What do I need to do in order to keep my code linked
together when the current working directory keeps changing?

I know that Robin has given you one suggestion for how to handle this. In my code, however, I've done it this way:

     curDir = os.getcwd()
     fileName = wxFileSelector("Open File", flags = wxOPEN | wxFILE_MUST_EXIST)
     if fileName == "": return
     fileName = os.path.join(os.getcwd(), fileName)
     os.chdir(curDir)

This results in 'filename' being set to the name and full path of the selected file, and the current directory isn't changed. It worked fine in my program...

  - Erik.