Problems with import of modules

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ilias Lazaridis

    #1

    Problems with import of modules

    I am within a directory

    \doc\template\

    I launch script.py

    within this script.py, I like to import a module from the doc directory.

    this here does not work:

    form ..\..\module_na me import this_one

    how do I go back in the directory hierarchy to import something?

    If this is not possible:

    How can I modify the python search-path from within the script, thus it
    contains the doc directory?

    ..

    --

  • Farshid Lashkari

    #2
    Re: Problems with import of modules

    > How can I modify the python search-path from within the script, thus it[color=blue]
    > contains the doc directory?[/color]

    Hi,

    The sys.path variable is a list of strings that contains the current
    module search path. You can add your own path to this list:

    import sys
    sys.path.append ('../')

    -Farshid

    Comment

    • Carl J. Van Arsdall

      #3
      Re: Problems with import of modules

      Ilias Lazaridis wrote:[color=blue]
      > I am within a directory
      >
      > \doc\template\
      >
      > I launch script.py
      >
      > within this script.py, I like to import a module from the doc directory.
      >
      > this here does not work:
      >
      > form ..\..\module_na me import this_one
      >[/color]
      Well, if you are in linux you can do this easily by changing your
      PYTHONPATH environment variable, either by changing it explicitely or by
      editing it in your .rc files to append the /doc directory.

      Although I don't know specifically where this variable might be if you
      are using windows, in either case(windows or linux), you can alter this
      from python using sys.path

      import sys
      sys.path.append ("/doc")

      Hope that helps,

      -carl
      [color=blue]
      > how do I go back in the directory hierarchy to import something?
      >
      > If this is not possible:
      >
      > How can I modify the python search-path from within the script, thus it
      > contains the doc directory?
      >
      > .
      >
      >[/color]


      --

      Carl J. Van Arsdall
      cvanarsdall@mvi sta.com
      Build and Release
      MontaVista Software

      Comment

      • Ilias Lazaridis

        #4
        Re: Problems with import of modules

        the sys.path.append has done the work.

        thanks.

        ..

        --

        Comment

        Working...