2017-02-06 77 views
-2

我的应用程序中的一些文件仅适用于python 3。如何忽略特定文件的特定Python版本

如果代码在python 2上运行,我可以忽略这些文件吗?

例子:

import sys 
if sys.version_info.major < 3: 
    return # this doesn't work 

<other code should to be ignored for python < 3> 

我知道我能做到这一点的方法:

import sys 
if sys.version_info.major < 3: 
    pass 
else: 
    <other code should to be ignored for python < 3> 

但问题是,我能避免内部包括else范围整个文件上下文?

另一个问题,我可以从__init__.py中排除这个文件吗?

+2

import sys from pypreprocessor import pypreprocessor #exclude if sys.version_info.major < 3: pypreprocessor.defines.append('python3') pypreprocessor.parse() #endexclude #ifdef python3 # <other code should to be ignored for python < 3> #endif 

请张贴其他的解决方案也许它更好地使在导入文件中导入语句在Python版本的条件。您可以简单地在这些文件中添加一个断言,以确保它们不会被错误的python版本意外导入。 –

+0

你是什么意思“我的模块中的文件”?你的意思是“我的包装中的模块”? –

+0

编辑的问题,它不是包。只需定期应用程序并感谢您的反对票。 – Kostanos

回答