在pycharm中运行python文件没有问题,切换到cmd中是提示:ImportError: No module named xxx

原因:

pycharm在运行时会把当前工程的所有文件夹路径都作为包的搜索路径,而命令行默认只是搜索当前路径。’

解决方法:

在出错的模块中加上

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
  • 2022-12-23
  • 2022-12-23
  • 2021-12-06
  • 2021-04-29
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-02
  • 2022-12-23
  • 2021-11-13
  • 2022-02-18
相关资源
相似解决方案