the problem of import module

Discussion in 'Python' started by fts2012@gmail.com, Mar 7, 2008.

  1. Guest

    follow the dive into python
    -----------------------------------------------------------------
    >>> import sys
    >>> sys.path
    >>> sys.path.append('E:\achieve\book\diveintopython-pdfzh-cn-5.4b\diveintopythonzh-cn-5.4b\py')

    -----------------------------------------------------------------
    I append the filepath of <<dive into python>>'s examples into
    sys.path,but
    -----------------------------------------------------------------
    >>> sys.path

    ['C:\\Python25\\Lib\\idlelib', 'C:\\WINDOWS\\system32\\python25.zip',
    'C:\\Python25\\DLLs', 'C:\\Python25\\lib', 'C:\\Python25\\lib\\plat-
    win', 'C:\\Python25\\lib\\lib-tk', 'C:\\Python25', 'C:\\Python25\\lib\
    \site-packages', 'E:\x07chieve\x08ook\\diveintopython-pdfzh-cn-5.4b\
    \diveintopythonzh-cn-5.4b\\py']
    >>> import fileinfo#fileinfo is a module in the path


    Traceback (most recent call last):
    File "<pyshell#5>", line 1, in <module>
    import fileinfo
    ImportError: No module named fileinfo

    -----------------------------------------------------------------
    Can anyone tell me the reason of the above and how to add paths to
    python path except adding them in the enviroment path.
    Thanks.
     
    , Mar 7, 2008
    #1
    1. Advertising

  2. Peter Otten Guest

    wrote:

    > follow the dive into python
    > -----------------------------------------------------------------
    >>>> import sys
    >>>> sys.path
    >>>> sys.path.append('E

    \achieve\book\diveintopython-pdfzh-cn-5.4b\diveintopythonzh-cn-5.4b\py')
    > -----------------------------------------------------------------
    > I append the filepath of <<dive into python>>'s examples into
    > sys.path,but
    > -----------------------------------------------------------------
    >>>> sys.path

    > ['C:\\Python25\\Lib\\idlelib', 'C:\\WINDOWS\\system32\\python25.zip',
    > 'C:\\Python25\\DLLs', 'C:\\Python25\\lib', 'C:\\Python25\\lib\\plat-
    > win', 'C:\\Python25\\lib\\lib-tk', 'C:\\Python25', 'C:\\Python25\\lib\
    > \site-packages', 'E:\x07chieve\x08ook\\diveintopython-pdfzh-cn-5.4b\
    > \diveintopythonzh-cn-5.4b\\py']
    >>>> import fileinfo#fileinfo is a module in the path

    >
    > Traceback (most recent call last):
    > File "<pyshell#5>", line 1, in <module>
    > import fileinfo
    > ImportError: No module named fileinfo
    >
    > -----------------------------------------------------------------
    > Can anyone tell me the reason of the above and how to add paths to
    > python path except adding them in the enviroment path.
    > Thanks.


    The path you append to sys.path is not properly escaped:

    >>> "E:\archive"

    'E:\x07rchive' # \a has become the single character chr(7)

    Use double backslashes or raw strings instead:

    >>> "E:\\archive"

    'E:\\archive'
    >>> r"E:\archive"

    'E:\\archive'

    When you print it the extra backslash will be gone:

    >>> print "E:\\archive" # \\ is the escape sequence for the backslash

    E:\archive

    Peter
     
    Peter Otten, Mar 7, 2008
    #2
    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.

Share This Page