【问题标题】:django can't import in installed apps and can't import functiondjango 无法在已安装的应用程序中导入,也无法导入函数
【发布时间】:2012-03-06 07:42:49
【问题描述】:

在 Django 中,我有一个“fbsurvey”项目,带有一个“画布”应用程序。

我有另一个“cblib”项目,有一个“调查”应用和一个“图表”应用。

在“调查”应用中,有模型和一些功能。 在“graphs”应用程序中,只有一个“utils”文件夹,其中包含 2 个 .py 文件——一个包含函数“get_chart_info”的文件“get_chart_info”和一个包含一些分类函数的文件“chart_utils”

graphs 应用在每个级别都有一个 init.py “调查”工作中的所有模型

但“get_chart_info”(文件)拒绝导入。

如果我尝试将“cblib.graphs”放入已安装的应用程序中,当我尝试运行服务器时,它会中断,并显示“错误:没有名为 graphs 的模块”

如果我将其排除在已安装的应用程序之外,我会得到: /canvas/chart/ 处的 ImportError 没有名为 graphs.utils.get_chart_info 的模块 (顺便说一句,我不明白为什么这说没有名为 graphs.utils 而不是 cblib.graphs.utils 的模块)

带有对导入语句的行引用。

请注意,所有导入都在 shell 中工作。 IE。当我运行时: ./manage.py 外壳 导入 cblib 导入 cblib.survey 导入 cblib.graphs 导入 cblib.graphs.get_chart_info 从 cblib.graphs.get_chart_info 导入 get_chart_info

没有失败。

有谁知道为什么这可能会破坏?我觉得我已经检查了所有内容。

有人提到查看我的项目的 ascii 树可能很有用(已针对相关性进行了编辑)

cblib 看起来像:

.
├── graphs
│   ├── admin.py
│   ├── __init__.py
│   ├── __init__.pyc
│   └── utils
│       ├── get_chart_info.py
│       ├── get_chart_info.pyc
│       ├── graph_utils.py
│       ├── graph_utils.pyc
│       ├── __init__.py
│       └── __init__.pyc
├── __init__.py
├── __init__.pyc
└── survey
    ├── admin.py
    ├── fixtures
    │   ├── badges.json
    │   ├── q1-174.json
    │   ├── q175-271.json
    │   ├── q272-302.json
    │   └── responseoptions_767-1594.json
    ├── __init__.py
    ├── __init__.pyc
    ├── management
    │   ├── commands
    │   │   ├── create_fake_users.py
    │   │   ├── import_fake_user_data.py
    │   │   ├── import_questions.py
    │   │   └── __init__.py
    │   └── __init__.py
    ├── migrations
    │   ├── 0001_initial.py
    │   ├── 0002_auto__del_field_votelog_direction.py
    │   ├── 0003_auto__chg_field_pointlog_action_type.py
    │   ├── 0004_auto__add_opengraphverb__add_field_question_school_specific_opengraph_.py
    │   └── __init__.py
    └── models
        ├── badge.py
        ├── badge.pyc
        ├── __init__.py
        ├── __init__.pyc
        ├── opengraphverb.py
        ├── opengraphverb.pyc
        ├── pointlog.py
        ├── pointlog.pyc
        ├── question.py
        ├── question.pyc
        ├── responseoption.py
        ├── responseoption.pyc

fbsurvey 看起来像:

.
├── canvas
│   ├── admin.py
│   ├── admin.pyc
│   ├── brainys.json
│   ├── data.csv
│   ├── decorators.py
│   ├── decorators.pyc
│   ├── DefaultInfoObject.py
│   ├── DefaultInfoObject.pyc
│   ├── DefaultJsonResponse.py
│   ├── DefaultJsonResponse.pyc
│   ├── fixtures
│   │   └── test-fixture.json
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── level.py
│   ├── level.pyc
│   ├── management
│   │   ├── commands
│   │   │   ├── convert_fbuser_to_cbuser.pyc
│   │   │   ├── credit_inviters.py
│   │   │   ├── __init__.py
│   │   │   ├── __init__.pyc
│   │   │   ├── reminder_wallposts.py
│   │   │   ├── reminder_wallposts.pyc
│   │   │   └── update_user_colleges.py
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── 0001_initial.pyc
│   │   ├── __init__.py
│   │   └── __init__.pyc
│   ├── models.py
│   ├── models.pyc
│   ├── static
│   │   ├── css
│   │   ├── img
│   │   └── js
│   ├── templates
│   │   ├── answers.html
│   │   ├── answers-mobile.html
│   │   ├── answertest.html
│   │   ├── badge-explanation.html
│   │   ├── badges.html
│   │   ├── baduser.html
│   │   ├── bottombar.html
│   │   ├── bottombar-mobile.html
│   │   ├── browse-stories.html
│   │   ├── end.html
│   │   ├── friends.html
│   ├── tests.py
│   ├── tests.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── views
│   │   ├── answers.py
│   │   ├── answers.pyc
│   │   ├── badge_explanation.py
│   │   ├── badge_explanation.pyc
│   │   ├── badges.py
│   │   ├── badges.pyc
│   │   ├── browse_stories.py
│   │   ├── browse_stories.pyc
│   │   ├── explanation.pyc
│   │   ├── format_for_graph.py
│   └── views.pyc
├── __init__.py
├── __init__.pyc
├── local_settings.py
├── local_settings.pyc
├── local_settings.py.example
├── logclient
│   └── __init__.py
├── manage.py
├── maps.py
├── maps.pyc
├── patch.py
├── pokesite
├── python.exe.stackdump
├── README
├── requirements.txt
├── settings.py
├── settings.pyc
├── survey
│   ├── admin.pyc
│   ├── data
│   │   ├── CBI Questions with percentages v3.csv
│   │   ├── data.csv
│   │   ├── List of School Nicknames.txt
│   │   ├── pquestions.csv
│   │   ├── question_pks_and_categories.csv
│   │   ├── questions.csv
│   │   └── questions.json
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── localsetting.py
│   ├── models.pyc
│   ├── tests.py
│   └── views.py
├── surveydump.json
├── sync_badges.py
├── templates
│   ├── 404.html
│   ├── 500.html
│   ├── base.html
│   └── base-mobile.html
├── testdump.json
├── tree.txt
├── urls.py
└── urls.pyc

【问题讨论】:

  • 你的 settings.py 是什么样的?你都添加了吗?
  • 我的 fbsurvey settings.py 在我添加图表应用程序之前没有改变(一切正常)。 Cblib 没有设置,因为它是一个库。
  • 我认为您可能需要在设置中注册应用程序。你的项目怎么知道从哪里拉?您可能想弄清楚项目的整体架构。也许这会有所帮助:stackoverflow.com/questions/2069254/…
  • 好吧,正如我所说,“如果我尝试将“cblib.graphs”放入已安装的应用程序中,当我尝试运行服务器时,它会中断,并显示“错误:没有名为 graphs 的模块”跨度>
  • 所以当你说你放“cblib.graphs”时,cblib 是它自己的项目,但你想通过 fbsurvey 访问应用程序,对吗?您必须将应用程序从 cblib 复制到 fbsurvey,然后在 fbsurvey 的 settings.py 中注册应用程序,或者使用上述链接中提到的 PYTHONPATH hack 在设置中注册应用程序。您可以随时在 shell 中检查类似的内容(项目中的“python manage.py shell”)

标签: python django


【解决方案1】:

答案与我的 .pyc 文件有关...我不知道如何或为什么,但正在运行

找到 . -name "*.pyc" -delete

(然后大概重新生成了我的 pyc 文件) 在我的两个项目目录中都解决了这个问题。

【讨论】:

  • 这个命令只是在错误的地方删除旧的未使用的.pyc文件并且不会生成新的
  • 但是当我运行我的应用程序时会生成新的。
猜你喜欢
  • 1970-01-01
  • 2016-11-26
  • 2018-07-21
  • 1970-01-01
  • 2018-07-22
  • 1970-01-01
  • 2020-04-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多