Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > the problem of import module

Reply
Thread Tools

the problem of import module

 
 
fts2012@gmail.com
Guest
Posts: n/a
 
      03-07-2008
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.
 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      03-07-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
imp.find_module don't found my module but standard import statement can import this module… why ? Stéphane Klein Python 0 07-05-2011 07:36 AM
Why 'import module' will not import module.py but the directorymodule? Peng Yu Python 0 10-31-2009 11:51 PM
Handling import conflicts when module has the same name as a library module that it needs to import? plb Python 0 02-08-2005 01:08 PM
Handling import conflicts when module has the same name as a library module that it needs to import? plb Python 0 02-08-2005 01:01 PM
import vs from module import : any performance issue? Pierre Rouleau Python 4 03-07-2004 05:06 PM



Advertisments