Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
文件"&书信电报;字符串>&引用;python中的回溯_Python_Python 2.7_Python Import_Traceback - Fatal编程技术网

文件"&书信电报;字符串>&引用;python中的回溯

文件"&书信电报;字符串>&引用;python中的回溯,python,python-2.7,python-import,traceback,Python,Python 2.7,Python Import,Traceback,我正在重构一个巨大的Py模块到包——不破坏现有代码,我把它的内容移到了包/y-iNITSy.Py < /Cult>模块(),然后从那里开始分裂。我注意到在我的回溯中我得到: 回溯(最近一次呼叫最后一次): 文件“”,第656行,在DoItemMenu中 弹出菜单中第2109行的文件“bash\balt.py” link.AppendToMenu(菜单,父项,*args) 附录菜单中2225行的文件“bash\balt.py” 对于self.links中的链接:link.AppendToMenu(

我正在重构一个巨大的Py模块到包——不破坏现有代码,我把它的内容移到了<代码>包/y-iNITSy.Py < /Cult>模块(),然后从那里开始分裂。我注意到在我的回溯中我得到:

回溯(最近一次呼叫最后一次):
文件“”,第656行,在DoItemMenu中
弹出菜单中第2109行的文件“bash\balt.py”
link.AppendToMenu(菜单,父项,*args)
附录菜单中2225行的文件“bash\balt.py”
对于self.links中的链接:link.AppendToMenu(子菜单、窗口、数据)
...
其中,
文件“
中的行对应于特定的
包/\uuuuu init\uuuuuuu.py
模块。此外,PyCharm的调试器会显示一行“frame not available”(帧不可用),并且不会进入
\uuu init\uuuu.py
中的行。为什么?它与导入模式有关吗

代码由以下程序导入:


代码不是以传统方式导入的;相反,使用用于加载
\uuuu init\uuuu.py
文件的

将启动程序
load_module()
函数中的流分成一个包(因此不是模块的路径),可以得到以下结果:

# the fullname module isn't yet loaded
sys.modules[fullname] = imp.new_module(fullname)
initfile = '__init__'  # or u'__init__' if a unicode path was used

# if no .py file was found, so not a module
mod = sys.modules[fullname]
mod.__loader__ = self
mod.__file__ = os.path.join(os.getcwd(),filename)
mod.__path__ = [filename]
#init file
initfile = os.path.join(filename,initfile+ext)
if os.path.exists(initfile):
    with open(initfile,'U') as fp:
        code = fp.read()
    exec code in mod.__dict__
return mod
这将创建一个空模块对象,手动加载源代码并将其作为字符串执行,并将模块名称空间作为已执行代码的全局变量传入。生成的代码对象总是在回溯中列出

>>> import imp
>>> mod = imp.new_module('foo.bar')
>>> mod.__file__ = r'C:\some\location\foo\bar'
>>> mod.__path__ = [r'foo\bar']
>>> exec 'raise ValueError("oops")' in mod.__dict__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1, in <module>
ValueError: oops
请注意,我在文件名中包含了
\uuuu init\uuuuuuuuy.py
;将其转换回您将使用的启动器:

if os.path.exists(initfile):
    with open(initfile,'U') as fp:
        code = fp.read()
    exec compile(code, initfile, 'exec') in mod.__dict__

这意味着Python被告知从字符串编译该模块;这可能是PyCharm的错,而不是Python。你现在到底是如何运行这段代码的?@MartijnPieters:是启动器吗?我几乎可以肯定,在复制粘贴文件之前,它没有加载字符串…不,我的意思是PyCharm指示Python解释器从字符串运行该文件,而不是要求Python将其作为模块导入。我没有说模块本身在做任何事情。@MartijnPieters:当我直接运行启动器时也会发生这种情况-我提到了Pycharm,因为在我看来,这与它无法在
\uuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu谢谢:只需确定我得到它-使用Culiple()函数只会在我的场景中起作用(记住我在重构的中间,所以这些文件很大)?顺便说一句,我注意到没有pyc文件用于
\uuuu init\uuuu.py
,只是在问题中没有提到这一点-现在我知道了原因。
exec
无论如何都必须编译,但是通过使用显式的
compile()
调用,可以为生成的代码对象提供一个文件名。这将有助于启动程序启动的所有代码。
>>> exec compile('raise ValueError("oops")', r'C:\some\location\foo\bar\__init__.py', 'exec') in mod.__dict__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\some\location\foo\bar\__init__.py", line 1, in <module>
ValueError: oops
if os.path.exists(initfile):
    with open(initfile,'U') as fp:
        code = fp.read()
    exec compile(code, initfile, 'exec') in mod.__dict__