the problem of import module

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • fts2012@gmail.com

    #1

    the problem of import module

    follow the dive into python
    -----------------------------------------------------------------
    >>import sys
    >>sys.path
    >>sys.path.appe nd('E:\achieve\ book\diveintopy thon-pdfzh-cn-5.4b\diveintopy thonzh-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\\s ystem32\\python 25.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\x 08ook\\diveinto python-pdfzh-cn-5.4b\
    \diveintopython zh-cn-5.4b\\py']
    >>import fileinfo#filein fo 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.
  • Peter Otten

    #2
    Re: the problem of import module

    fts2012@gmail.c om wrote:
    follow the dive into python
    -----------------------------------------------------------------
    >>>import sys
    >>>sys.path
    >>>sys.path.app end('E
    \achieve\book\d iveintopython-pdfzh-cn-5.4b\diveintopy thonzh-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\\s ystem32\\python 25.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\x 08ook\\diveinto python-pdfzh-cn-5.4b\
    \diveintopython zh-cn-5.4b\\py']
    >>>import fileinfo#filein fo 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:\\archiv e"
    'E:\\archive'
    >>r"E:\archiv e"
    'E:\\archive'

    When you print it the extra backslash will be gone:
    >>print "E:\\archiv e" # \\ is the escape sequence for the backslash
    E:\archive

    Peter

    Comment

    Working...