简体   繁体   English

Python3.7-即使在sys.path中列出,也找不到模块

[英]Python3.7 - module not found even though it is listed in sys.path

I try to import some custom modules/packages but get the error "ModuleNotFoundError: No module named 'reader'". 我尝试导入一些自定义模块/程序包,但收到错误“ ModuleNotFoundError:没有名为'reader'的模块”。

I made sure that the absolute path to my custom package directory is listed in sys.path: 我确保在sys.path中列出了我的自定义软件包目录的绝对路径:

>>> os.getcwd()
'C:\\Python Projects\\reader'
>>> sys.path.append('C:\\Python Projects\\reader')
>>>
>>> sys.path
['', 'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37\\python37.zip', 'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37\\DLLs', 'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37\\lib', 'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37', 'C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python37\\lib\\site-packages', 'C:\\Python Projects\\reader']
>>>
>>> import reader
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'reader'

Also, all the paths listed in sys.path (except for '' and C:\\Python Projects\\reader) have been added in Environment variables > System variables (I am using Win10). 另外,sys.path中列出的所有路径(''和C:\\ Python Projects \\ reader除外)都已添加到环境变量>系统变量中(我正在使用Win10)。

Is there something else I should to in order to successfully import custom packages in my projects? 为了在项目中成功导入自定义包,我还应该做些其他事情吗?

Thank you 谢谢

sys.path.append('C:\\Python Projects')

Then import reader will look for reader under 'C:\\Python Projects' , ie it will test for 'C:\\Python Projects\\reader' . 然后import reader将在'C:\\Python Projects'下寻找reader ,即它将测试'C:\\Python Projects\\reader' Currently it's testing for 'C:\\Python Projects\\reader\\reader' . 目前正在测试'C:\\Python Projects\\reader\\reader'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM