在ide中执行python程序,都已经在默认的项目路径中,所以直接执行是没有问题的。

但是在cmd下执行程序,所在路径是python的搜索路径,如果涉及到import引用就会报类似ImportError: No module named xxx这样的错误,解决方法:

在报错的模块中添加:

import sys
import os
curPath = os.path.abspath(os.path.dirname(__file__)) #获取当前绝对路径
rootPath = os.path.split(curPath)[0] #获取当前目录的上一级目录路径
sys.path.append(rootPath)

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-11-29
  • 2021-08-03
  • 2021-05-18
  • 2021-12-24
  • 2021-11-21
猜你喜欢
  • 2021-04-29
  • 2022-01-01
  • 2021-12-10
  • 2022-02-25
  • 2022-12-23
  • 2021-08-16
  • 2021-12-11
相关资源
相似解决方案