linux/cmd中python路径导入ModuleNotFoundError: No module named 'xxx'
import os
import sys
curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = os.path.split(curPath)[0]
print(rootPath)
sys.path.append(os.path.split(rootPath)[0])
from sendWeatherEmail.getWeatherInfo import Weather
在linux或者直接在cmd中运行python文件时 会遇到导包错误的情况
ModuleNotFoundError: No module named 'xxx’
由于不在pycharm中 所以这里不能将上一级目录加入 sources root
方法1:所以只能在导入前加入 路径的位置 于sys中
import os
import sys
curPath = os.path.abspath(os.path.dirname(__file__)) rootPath = os.path.split(curPath)[0] sys.path.append(os.path.split(rootPath)[0])
加入这3句话 然后在 from C import xxx as xxx
-----------------------------------------------------------------------------------------------------------
方法2:如果以上方法不能解决 可以直接将上一级目录路径加入sys
目录的路径为A/B/C
import os
import sys
|
1
|
<br>sys.path.append("A/B") |
from C import xxx as xxx
解决;
不太相信弯道超车,更欣赏仰望星空与脚踏实地
[LeetCode] 502. IPO 上市
[LeetCode] 495. Teemo Attacking 提莫攻击
[LeetCode] 655. Print Binary Tree 打印二叉树
[LeetCode] 654. Maximum Binary Tree 最大二叉树
[LeetCode] 637. Average of Levels in Binary Tree 二叉树的层平均值
Dubbo 在maven项目中的应用
前后端分离springmvc和RESTful理解
一个相对通用的JSON响应结构,其中包含两部分:元数据与返回值
MAC OS查看端口占用情况及杀死进程