Python中包无法使用问题
我在计算机建立了d:\bag文件夹,并创建了__init__.py文件,并保存在D:\bag下。在已经创建bag包中创建了一个名称time的模块。
运行以下代码出错,请给指导一下。
import bag.time
if name==’main‘:
print(bag.time.minutes)
报错为Traceback (most recent call last):
File “C:\Program Files\149-3.py”, line 1, in
import bag.time
ModuleNotFoundError: No module named ‘bag’
谢谢
关于 LearnKu
change
__init__.pyyto__init__.py.确认
d:\路径有在以下的输出结果中如果没有, 你会得到
ModuleNotFoundError: No module named 'bag'For WIN10,
['', 'C:\Program Files\Lib\idlelib', 'D:\bag', 'C:\Program Files\python39.zip', 'C:\Program Files\DLLs', 'C:\Program Files\lib', 'C:\Program Files', 'C:\Program Files\lib\site-packages']
仍出现找不到模块的错误 Traceback (most recent call last): File "D:\bag\149-6.py", line 3, in from bag import time ModuleNotFoundError: No module named 'bag' 谢谢您了。
pycharm打开项目后 在pycharm里右键bag文件夹 选择一下 Mark Directory as 然后选 Sources Root 试一下
还是原来的错误提示,谢谢大家了。
If you run the script by python directly, not under
PyCharm/VS Code/Virtual Environmentor else.