VMx

vmx.im
啥都学一点的填坑人
  1. 首页
  2. python
  3. 正文

python在vscode或命令行出现ModuleNotFoundError问题

2021年3月14日 837点热度 0人点赞 0条评论

以前都是使用pycharm开发,没有遇到过,主要是pycharm运行的时候会自动将工作目录加到系统path中去。

而使用vscode或直接运行会报:

ModuleNotFoundError: No module named 'xxx' 错误
为了更深的理解工作目录,特分析了下:
目录结构如下:
# c.py
def say_hello():
    print("I am c.py")

# d.py
from usr.c.c import say_hello

if __name__ == "__main__":
    print("I am in d.py")
    print(sys.path)
    say_hello()

pycharm运行正常。

查看当path没有e:\git\dir目录时无法正常运行。

在前面加入

import sys
sys.path.append('e:\\git\\dir')
或者:
BASE_DIR=os.path.dirname(os.path.abspath(os.path.dirname(__file__)))
sys.path.insert(0,BASE_DIR)
才能解决。

 

Post Views: 432
标签: 暂无
最后更新:2021年3月15日

huifei

这个人很懒,什么都没留下

点赞

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

COPYRIGHT © 2021 vmx.im. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang