【问题标题】:Importing variables from python modules从 python 模块导入变量
【发布时间】:2018-09-04 09:05:30
【问题描述】:

我在 python 中有一个名为 tb_batch 的模块。内容是:

m_d = dt.datetime.now().strftime('%B') + '_' + str(dt.datetime.now().year)

当我尝试将此变量导入另一个 python 程序时,

from tb_batch import m_d

它抛出错误:

AttributeError: module 'tb_batch' has no attribute 'm_d'

我做错了什么?

【问题讨论】:

  • 无法重现错误,在我这边工作得很好(tb_batch.py 包含import datetime as dt 和你引用的行)。您可以为我们创建一个显示错误的Minimal, Complete, and Verifiable example 吗?
  • 我正在使用 jupyter notebook。当我在 Visual Studio 代码中尝试相同的代码时,它可以完美运行
  • AttributeError Traceback (most recent call last) <ipython-input-22-5e9764232617> in <module>() 1 import tb_batch ----> 2 print(tb_batch.tb_path) AttributeError: module 'tb_batch' has no attribute 'tb_path'
  • AttributeError Traceback (most recent call last) <ipython-input-20-e883f7ca7e7a> in <module>() ----> 1 tb_batch.m_d AttributeError: module 'tb_batch' has no attribute 'm_d'
  • 我对@9​​87654322@ 没有太多经验。请标记它,以便具有相关专业知识的人更容易找到您的问题。

标签: python jupyter-notebook python-import


【解决方案1】:

我认为这里的问题在于 jupyter 细胞。我已经在 VisualStudioCode 中运行了我的代码,并且运行良好。感谢大家的贡献

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-02-27
    • 1970-01-01
    • 1970-01-01
    • 2019-11-28
    • 2011-04-06
    相关资源
    最近更新 更多