问题标签 [python-import]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
4 回答
1093 浏览

python - Python 访问父对象实例

我目前正在尝试使用 PyGame 编写多文件 Python (2.6.5) 游戏。问题是其中一个文件“pyconsole.py”需要能够调用由主文件“main.py”导入的其他对象的实例上的方法。问题是我在主文件中有一个列表来保存所有游戏对象(玩家的船、敌船、车站等)的实例,但我似乎无法从该列表中调用方法“pyconsole.py”尽管我在from pyconsole import *主循环开始之前在“main.py”中做一个。这根本不可能吗,我是否应该使用 M4 将每个文件组合成 1 个单个文件,然后进行字节码编译和测试/分发?

例子:

如果我对 Python 名称、类和所有这些东西的理解有限,那么 NoneType 意味着名称test.bar.get()- 因此test.bar- 没有分配给任何东西。

0 投票
2 回答
731 浏览

python - 导入失败并出现一个奇怪的错误

我得到:

/blog/post/test 处的 TemplateSyntaxError 在渲染时捕获 NameError:未定义全局名称“forms”

对于此代码:
forms.py

模型.py

__init__.py

直接在init .py 中导入表单会导致:

AttributeError:“模块”对象没有属性“评论”

这是堆栈跟踪,错误似乎来自 dojango 但这并没有真正意义:

文件“I:\wamp\www\Website\Comments__init__.py”,第 1 行,来自 Website.Comments 导入模型,表格文件“I:\wamp\www\Website\Comments\forms.py”,第 1 行,在从 dojango 导入表单文件“C:\Python26\lib\site-packages\dojango\forms__init__.py”,第 2 行,从小部件导入 * 文件“C:\Python26\lib\site-packages\dojango\forms\widgets .py”,第 11 行,从 dojango.util.config 导入配置文件“C:\Python26\lib\site-packages\dojango\util\config.py”,第 3 行,从 dojango.util 导入媒体文件“ C:\Python26\lib\site-packages\dojango\util\media.py”,第 49 行,在 settings.INSTALLED_APPS 中的应用程序中)文件“C:\Python26\lib\site-packages\dojango\util\media. py",第 49 行,用于设置中的应用程序。INSTALLED_APPS) 文件“C:\Python26\lib\site-packages\dojango\util\media.py”,第 38 行,在 find_pp_dojo_dir_and_url media_dir = find_app_dojo_dir(app_name) 文件“C:\Python26\lib\site-packages\dojango \util\media.py",第 27 行,在 find_pp_dojo_dir base = find_app_dir(app_name) 文件 "C:\Python26\lib\site-packages\dojango\util\media.py",第 20 行,在 find_pp_dir mod =获取属性(在 find_pp_dir mod = getattr(在 find_pp_dir mod = getattr(导入(m, {}, {}, [a]), a)

评论应用程序位于已安装的应用程序中。我应该怎么办?

编辑:如果我尝试直接包含表单,import forms我会得到:

回溯(最后一次调用):文件“I:\wamp\www\Website\manage.py”,第 11 行,在 execute_manager(settings)
文件“C:\Python26\lib\site-packages\django\core\management__init__ .py”,第 438 行,在 execute_manager utility.execute()
文件“C:\Python26\lib\site-packages\django\core\management__init__.py”,第 379 行,在执行 self.fetch_command(subcommand).run_from_argv( self.argv)
文件“C:\Python26\lib\site-packages\django\core\management\base.py”,第 191 行,在 run_from_argv self.execute(*args, **options.dict )文件
“C: \Python26\lib\site-packages\django\core\management\base.py",第 209 行,在执行 translation.activate('en-us')
文件“C:\Python26\lib\site-packages\django\utils\translation__init__.py”,第 66 行,激活返回 real_activate(language)
文件“C:\Python26\lib\site-packages\django\utils\ functional.py”,第 55 行,在 _ curried return _curried_func(*(args+moreargs), **dict(kwargs, **morekwargs))
文件“C:\Python26\lib\site-packages\django\utils\translation__init__ .py”,第 36 行,在延迟加载程序中返回 getattr(trans, real_name)(*args, **kwargs)
文件“C:\Python26\lib\site-packages\django\utils\translation\trans_real.py”,l ine 193,在激活 _active[currentThread()] = translation(language)
文件“C:\Python26\lib\site-packages\django\utils\translation\trans_real.py”,第 176 行,翻译中 default_translation = _fetch(settings.LANGUAGE_CODE)
文件“C:\Python26\lib\site-packages \django\utils\translation\trans_real.py”,第 159 行,在 _fetch app = import_module(appname)
文件“C:\Python26\lib\site-packages\django\utils\importlib.py”,第 35 行,在im port_module import (name)
File "I:\wamp\www\Website\Comments__init__.py", line 2, in import forms
文件 "I:\wamp\www\Website\Comments\forms.py", line 3, in从 dojango.forms 导入字段,小部件
文件“C:\Python26\lib\site-packages\dojango\forms__init__.py”,第 2 行,从小部件导入 *
文件“C:\Python26\lib\site-packages\dojango\forms\widgets.py”,第 11 行,从 dojango.util.config 导入配置
文件“C:\Python26\lib\site-packages\dojango\util \config.py",第 3 行,从 dojango.util 导入媒体
文件 "C:\Python26\lib\site-packages\dojango\util\media.py",第 49 行,在 settings.INSTALLED_APPS 中的应用程序
文件中“C:\Python26\lib\site-packages\dojango\util\media.py”,第 49 行,在 settings.INSTALLED_APPS 中的应用程序中)
文件“C:\Python26\lib\site-packages\dojango\util\media .py”,第 38 行,在 find_a pp_dojo_dir_and_url media_dir = find_app_dojo_dir(app_name)
文件“C:\Python26\lib\site-packages\dojango\util\media.py”中,第 27 行,在 find_a pp_dojo_dir base = find_app_dir(app_name)
文件“C:\Python26\lib\site-packages\dojango\util\media.py”,第 20 行,在 find_a pp_dir mod = getattr( import (m, {}, {}, [a]), a)
AttributeError :“模块”对象没有属性“评论”

删除对 dojango 的任何引用可以解决问题。

0 投票
4 回答
21526 浏览

python - python导入错误

我的进口有什么问题?

应用文件夹结构:

我的应用程序/

  • 模型/models.py 包含SpotModel()
  • tests/tests.py 包含 TestSpotModel(unittest.TestCase)。tests.py 导入from myapp.models.models import *,就像一个魅力
  • 脚本/import.py 包含from myapp.models.models import *

问题是 import.py 在执行时会导致错误:

但是 tests.py 运行。

我在 , 和 中有文件,__init__.py如前所述,使用 nosetests 运行单元测试可以按预期工作。myapp/__init__.pymyapp/models/__init__.pymyapp/tests/__init__.py

0 投票
6 回答
32362 浏览

python - Python:为什么要'从进口*'被禁止?

如果你碰巧有

在您的程序(或模块)的中间,您会收到警告:

我理解为什么import *一般不鼓励(命名空间不可见),但在许多情况下它会被证明是方便的,特别是在代码不与任何人共享的情况下。

那么,谁能详细解释为什么from <module> import *在所有可能的情况下都应该被禁止?

0 投票
2 回答
106 浏览

python - 我怎样才能得到 'urlpatterns = __import__()' 像正常的导入语句一样工作?

我正在尝试创建一个可插入其他项目的导入语句。该语句位于 urls.py

所以这有效:

但这不起作用:

我怎样才能让后者工作?

0 投票
5 回答
16669 浏览

python - "no matching architecture in universal wrapper" problem in wxPython?

I am running Python 2.7 under Mac OS 10.6.4, and I just installed wxPython from the wxPython2.8-osx-unicode-2.8.11.0-universal-py2.7.dmg binary. I am getting a weird error on the import wx line in my Python scripts. FYI, I can import the wx module just fine from PyCrust. I don't really see what I have done wrong here. Could anyone please assist?

0 投票
6 回答
49685 浏览

python - 应该避免通配符导入吗?

我正在使用 PyQt 并遇到了这个问题。如果我的导入语句是:

然后 pylint 给出数百个“未使用的导入”警告。我很犹豫是否要关闭它们,因为可能还有其他未使用的导入实际上很有用。另一种选择是这样做:

我最终在 QtGui 线上有 9 个课程。还有第三种选择,即:

然后在我使用它们时为所有类添加 QtCore 或 QtGui 前缀。

在这一点上,我不知道我最终在我的项目中做了哪一个,尽管从我的角度来看,最后一个似乎是最痛苦的。这里的常见做法是什么?使用一种风格而不是另一种风格有技术上的理由吗?

0 投票
6 回答
23419 浏览

python - 如何在 Python 模块中正确使用相对或绝对导入?

在 Python 中使用相对导入有一个缺点,您将无法再将模块作为独立模块运行,因为您会遇到异常:ValueError: Attempted relative import in non-package

我应该如何修改示例代码以便能够执行 all test.py:foo.pybar.py

我正在寻找适用于 python 2.6+(包括 3.x)的解决方案。

0 投票
4 回答
27727 浏览

python - 为什么 Python 模块有时不导入它们的子模块?

我今天注意到一些奇怪的事情,我想解释一下。我不是 100% 确定如何将其表述为一个问题,所以谷歌是不可能的。由于某些奇怪的原因,日志记录模块无权访问模块 logging.handlers。不信你自己试试:

谁能解释为什么会这样?

0 投票
3 回答
12676 浏览

python - __future__ import with_statement 需要哪个 python 版本?

使用 python 2.6.5,我可以使用该with语句而无需调用from __future__ import with_statement. 我如何知道哪个版本的 Python 支持with而不专门从中导入它__future__