【问题标题】:Import error in linux but success on windows在linux中导入错误但在windows上成功
【发布时间】:2018-07-17 09:46:22
【问题描述】:
import os
os.chdir('../../')
print os.getcwd()
import sys
import unittest
from chatbot_run.chatbot_conf import chatbot_conf_dict

/home/mengyuguang/yige
Traceback (most recent call last):
  File "test_time_entity.py", line 21, in <module>
    from chatbot_run.chatbot_conf import chatbot_conf_dict
ImportError: No module named chatbot_run.chatbot_conf

chatbot_run 是我编写的一个项目。 我正在导入在 Windows 上运行良好的第五行。 python 文件位于 yige/test/ner 文件夹中,我也在此文件夹中运行该文件。 它打印了 yige,chatbot_run 也在 yige 中。 这真的很奇怪。导致这种情况的linux有什么区别。

【问题讨论】:

  • 将您的错误插入问题中
  • 谢谢,你能弄清楚发生了什么吗?
  • 你的linux运行机上好像没有安装包
  • ImportError: No module named chatbot_run.chatbot_conf 那是你的问题 - 在你的 windows 机器上导入配置文件失败.. 可以导入吗?
  • @eamirgh 这是我写的一个项目。

标签: python linux windows


【解决方案1】:

您应该将项目路径添加到sys.path,而不是使用os.chdir

sys.path.append("../..")
from chatbot_run.chatbot_conf import chatbot_conf_dict

【讨论】:

  • 它有效,但为什么它在使用 os.chdir 的 Windows 上有效?
  • Windows 中的大多数 IDE 会自动为您将项目路径添加到 sys.path。我怀疑是这种情况。
  • 是的,你是对的,我打印了路径,看到pycharm自动将我的项目路径添加到了sys.path。
猜你喜欢
  • 1970-01-01
  • 2020-10-24
  • 2013-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-10
  • 1970-01-01
  • 2018-08-25
相关资源
最近更新 更多