I hava tried to debug this and found something maybe helpful to it.
when I start python console . and import the module lib
,it works.then I check the sys.path, the result is like this.
>> import lib
>> import sys
>> sys.path
['', 'C:\\Python\\Anaconda3\\python37.zip', 'C:\\Python\\Anaconda3\\DLLs', 'C:\\Python\\Anaconda3\\lib', 'C:\\Python\\Anaconda3', 'C:\\Python\\Anaconda3\\lib\\site-packages', '', 'C:\\Python\\Anaconda3\\lib\\site-packages\\win32', 'C:\\Python\\Anaconda3\\lib\\site-packages\\win32\\lib', 'C:\\Python\\Anaconda3\\lib\\site-packages\\Pythonwin']
the first element in the list is ''
. then I set breakpoint in the python file before it import the module lib
.
file test_module_001.py:
import pdb;pdb.set_trace()
import lib
then I execute python tests\test_modulexx\test_module_001.py in the windows console and print sys.path
>f:\src\tests\test_modulexx\test_module_001.py(2)<module>()
->import lib
(Pdb)import sys
(Pdb)sys.path
['F:\\src\\tests\\test_modulexx', 'C:\\Python\\Anaconda3\\python37.zip', 'C:\\Python\\Anaconda3\\DLLs', 'C:\\Python\\Anaconda3\\lib', 'C:\\Python\\Anaconda3', 'C:\\Python\\Anaconda3\\lib\\site-packages', 'C:\\Python\\Anaconda3\\lib\\site-packages\\win32', 'C:\\Python\\Anaconda3\\lib\\site-packages\\win32\\lib', 'C:\\Python\\Anaconda3\\lib\\site-packages\\Pythonwin']
(Pdb)import lib
*** ModuleNotFoundError: No module named 'lib'
the first element in sys.path
has been changed. now it is F:\\src\\tests\\test_modulexx
. I add ''
to sys.path
and import lib
. it works.
(Pdb)sys.path.append('')
(Pdb)import lib
(Pdb)
I think the reason is that Scope of Python importing module is defined in the sys.path. and the ''
means the current path.