Python – ImportError: No module named abc

importpython

I created a class named abc in the python26 folder. I tried to refer it through another file

def FileRW():
   import re
   import os
   import abc

I am getting error Traceback (most recent call last):
File "", line 1, in
FileRW()
File "C:\Python26\pyFileIOprog", line 4, in FileRW
import ChangeList
ImportError: No module named abc

Could anybody please tell me as where I am going wrong. I have setup environment variables 'path' to C:\python26

Best Answer

abc is the name of a standard library module distributed with Python, so I strongly suggest you change the name to something that is unique.

Assuming you've done that, you can either put the module's file(s) in the same directory as the script that imports it and it will be found. If you want to put it somewhere else, you can append the path to its location to the sys.path variable. Here's an example:

import sys
sys.path.append('path/to/my/module')
# on Windows append something like 'C:\\path\\to\\my\\module' or r'C:\path\to\my\module'  

import my_abc  # should work now

...

Note that the above won't work unless you change the module's name because Python will find the standard module before it looks in the directory path you've appended. You could override that by inserting your module's path at the beginning of the sys.path list, but again I don't recommend doing that.

If you'd like your module's path automatically appended to the system's module search path, you can create a name.pth file and put it in one of four special directories. See the online documentation for the site.py file for the details.