Maybe a dumb question

Gary Herron gherron at islandtraining.com
Tue Sep 10 21:28:02 EDT 2002


On Tuesday 10 September 2002 02:58 pm, Fabien HENON wrote:
> I have written an editor for Povray using Python, Tkinter and Pmw
>
> It is available at :
>
> http://pyvon.sourceforge.net
>
> The software includes one main file and 6 others which are for the
> language customization. People can choose between French, English,
> German,...
> All the translations for these languages are stored in files called
> menu_francais.py, menu_english.py, menu_deutsch.py ....when the editor
> is started.
>
> I would like to clean the directory a bit and put all these 'languages
> files' into a sub-directory called Lang.
> How do I tell python to look into a particular sub-directory when
> importing a file.
>
> I tried the os.path.join syntax, but to no avail.

Two possibilities:

1.  You can manipulate the search path for imports thus:

    import sys
    langDir = '...whatever...'
    sys.path.append(langDir)
    import Whatever

  where the calculation of the path to the language directory is
  something like:

    thisPyFile = os.path.join(os.getcwd(), sys.argv[0])
    thisDir = os.path.normpath(os.path.dirname(thisPyFile))
    langDir = os.path.join(thisDir, 'Lang')


2.  Use the package interface for the directory Lang:

  Create an empty file Lang/__init__.py.  Then you can import
  Lang/menu_francais.py like this:

    import Lang.menu_francais.py

  Look through the standard library for many examples of (non empty)
  __init__.py files.

Gary Herron





More information about the Python-list mailing list