【问题标题】:I'm getting Import Error: No Module Named ecommerce.shipping in Python我收到导入错误:Python 中没有名为 ecommerce.shipping 的模块
【发布时间】:2021-07-28 04:30:24
【问题描述】:

I'm trying to import the "shipping" module in "try.py" but I'm getting this error.

  Traceback (most recent call last):
  File "try.py", line 1, in <module>
    from ecommerce.shipping import calc_shipping
ImportError: No module named ecommerce.shipping

你认为问题是什么? 这是我的项目结构的图像 [1]:https://i.stack.imgur.com/F8L31.png

【问题讨论】:

  • 您是否在电子商务文件夹中运行此脚本?如果是这样,请尝试from .shipping import calc_shipping
  • 现在我得到了这个:ValueError: Attempted relative import in non-package 这是否意味着,python 看不到电子商务不是一个包?

标签: python import module importerror traceback


【解决方案1】:

如果它与脚本在同一目录中,您可以像这样直接导入它:

from shipping import calc_shipping

【讨论】:

  • 谢谢,它可以正常工作,我想知道为什么它会发出警告。未解决的导入'运输'Python(未解决的导入)
  • 能分享一下 shipping.py 的结构吗?
  • 我在问题中分享了整个结构的图像。对于运输文件,我只有一个简单的函数,如下所示: def calc_shipping(): print("This is a Shipping Module")
  • 不确定,因为使用相同的结构我没有收到任何类型的警告
【解决方案2】:

你的代码应该

from .shipping import calc_shipping 

模块在同一个应用程序中,因此您必须使用“.”键入上面的代码。点指的是同一个应用程序中的文件。

【讨论】:

  • 它给出了这个:ValueError: Attempted relative import in non-package
【解决方案3】:

在这种情况下,请务必检查以下事项:

  1. 如果模块有 init,换句话说,如果它是一个 python 模块
  2. 检查它是否可以从父目录访问,在这个例子中是基础。
from .shipping import calc_shipping 

试试这个

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-08
    • 2017-03-18
    • 2015-02-09
    • 1970-01-01
    • 2016-07-11
    • 2013-03-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多