Problems with import of modules

Discussion in 'Python' started by Ilias Lazaridis, Jan 23, 2006.

  1. 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_name 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?

    ..

    --
    http://lazaridis.com
     
    Ilias Lazaridis, Jan 23, 2006
    #1
    1. Advertising

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


    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
     
    Farshid Lashkari, Jan 23, 2006
    #2
    1. Advertising

  3. Ilias Lazaridis wrote:
    > 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_name import this_one
    >

    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

    > 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?
    >
    > .
    >
    >



    --

    Carl J. Van Arsdall

    Build and Release
    MontaVista Software
     
    Carl J. Van Arsdall, Jan 23, 2006
    #3
  4. Ilias Lazaridis, Jan 27, 2006
    #4
    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. Remy Cool
    Replies:
    1
    Views:
    444
    Remy Cool
    Aug 27, 2003
  2. Tobiah
    Replies:
    2
    Views:
    322
    Tobiah
    Sep 14, 2003
  3. Ben Weintraub

    Disabling modules using Modules/Setup

    Ben Weintraub, Sep 9, 2006, in forum: Python
    Replies:
    0
    Views:
    361
    Ben Weintraub
    Sep 9, 2006
  4. Peter Peyman Puk

    Importing v reloading modules modules

    Peter Peyman Puk, Mar 19, 2010, in forum: Python
    Replies:
    0
    Views:
    314
    Peter Peyman Puk
    Mar 19, 2010
  5. ImpalerCore
    Replies:
    0
    Views:
    879
    ImpalerCore
    Mar 10, 2011
Loading...

Share This Page