The Famous Error Message: "ImportError: No module named python_script"

Eric Pederson eric.pederson at gmail.com
Thu Dec 14 02:16:04 EST 2006


rich murphy wrote:

>So, I assumed "the current directory" is C:\Python25 which did not
>work. Then I placed the fibo.py file in C: director. That did not work
>either. What directory does it mean then?
>
OK, forgive me for using 2.4...  Can you import "sys"?  Assuming you've 
got python_script.py at path:  "C:\\python_script.py" you might try this 
quick test:
  
 >>> import sys
 >>> sys.path
['C:\\Python24\\Lib\\idlelib', 'C:\\windows\\system32\\python24.zip', 
'C:\\Python24', 'C:\\Python24\\DLLs', 'C:\\Python24\\lib', 
'C:\\Python24\\lib\\plat-win', 'C:\\Python24\\lib\\lib-tk', 
'C:\\Python24\\lib\\site-packages', 
'C:\\Python24\\lib\\site-packages\\win32', 
'C:\\Python24\\lib\\site-packages\\win32\\lib']   ## your ouput may be 
different
 >>> sys.path.append("C:\\")
 >>> sys.path
['C:\\Python24\\Lib\\idlelib', 'C:\\windows\\system32\\python24.zip', 
'C:\\Python24', 'C:\\Python24\\DLLs', 'C:\\Python24\\lib', 
'C:\\Python24\\lib\\plat-win', 'C:\\Python24\\lib\\lib-tk', 
'C:\\Python24\\lib\\site-packages', 
'C:\\Python24\\lib\\site-packages\\win32', 
'C:\\Python24\\lib\\site-packages\\win32\\lib', 'C:\\']
 >>> import python_script


    6.1.1 The Module Search Path

When a module named spam is imported, the interpreter searches for a 
file named spam.py in the current directory [...]

Actually, modules are searched in the list of directories given by the 
variable |sys.path| which is initialized from the directory containing 
the input script (or the current directory), PYTHONPATH and the 
installation-dependent default. This allows Python programs that know 
what they're doing to modify or replace the module search path. Note 
that because the directory containing the script being run is on the 
search path, it is important that the script not have the same name as a 
standard module, or Python will attempt to load the script as a module 
when that module is imported. This will generally be an error. See 
section 6.2 <#standardModules>, ``Standard Modules,'' for more information.





More information about the Python-list mailing list