python setup.py py2exe语法无效(asyncsupport.py,第22行)

时间:2017-02-08 22:00:07

标签: python py2exe

此命令在我的个人计算机上正常工作,但在我的工作PC上一直给我这个错误。会发生什么事?我可以直接在Powershell中运行Char_Limits.py脚本而不会出现问题。     错误:编译' C:\ ProgramData \ Anaconda2 \ lib \ site-packages \ jinja2 \ asyncsupport.py'失败          SyntaxError:语法无效(asyncsupport.py,第22行)

我的setup.py文件如下所示:

from distutils.core import setup
import py2exe

setup (console=['Char_Limits.py'])

我的文件如下:

import xlwings as xw
from win32com.client import constants as c
import win32api

""" 
Important Notes: Header row has to be the first row. No columns without a header row. If you need/want a blank column, just place a random placeholder
header value in the first row.
Product_Article_Number column is used to determine the number of rows. It must be populated for every row.
"""

#functions, hooray!
def setRange(columnDict, columnHeader):
    column = columnDict[columnHeader]
    rngForFormatting = xw.Range((2,column), (bttm, column))
    cellReference = xw.Range((2,column)).get_address(False, False)
    return rngForFormatting, cellReference

def msg_box(message):
    win32api.MessageBox(wb.app.hwnd, message)   

#Character limits for fields in Hybris
CharLimits_Fields = {"alerts":500, "certifications":255, "productTitle":300,
        "teaserText":450 , "includes":1000, "compliance":255, "disclaimers":9000, 
        "ecommDescription100":100, "ecommDescription240":240, 
        "internalKeyword":1000, "metaKeywords":1000, "metaDescription":1000,
        "productFeatures":7500, "productLongDescription":1500,"requires":500,
        "servicePlan":255, "skuDifferentiatorText":255, "storage":255, 
        "techDetailsAndRefs":12000, "warranty":1000}

# Fields for which a break tag is problematic.  
BreakTagNotAllowed = ["ecommDescription100", "ecommDescription240", "productTitle", 
                        "skuDifferentiatorText"]    

app = xw.apps.active                        
wb = xw.Book(r'C:\Users\XXXX\Documents\Import File.xlsx')

#identifies the blanket range of interest
firstCell = xw.Range('A1')
lstcolumn = firstCell.end("right").column

headers_Row = xw.Range((1,1), (1, lstcolumn)).value
columnDict = {}

for column in range(1, len(headers_Row) + 1):
    header = headers_Row[column - 1]
    columnDict[header] = column


try:
    articleColumn = columnDict["Product_Article_Number"]

except: 
    articleColumn = columnDict["Family_Article_Number"]

firstCell = xw.Range((1,articleColumn))

bttm = firstCell.end("down").row

wholeRange = xw.Range((1,1),(bttm, lstcolumn))
wholeRangeVal = wholeRange.value

#Sets the font and deletes previous conditional formatting
wholeRange.api.Font.Name = "Arial Unicode MS"
wholeRange.api.FormatConditions.Delete()

for columnHeader in columnDict.keys():
    if columnHeader in CharLimits_Fields.keys():
        rng, cellRef = setRange(columnDict, columnHeader)
        rng.api.FormatConditions.Add(2,3, "=len(" + cellRef + ") >=" + str(CharLimits_Fields[columnHeader]))
        rng.api.FormatConditions(1).Interior.ColorIndex = 3

    if columnHeader in BreakTagNotAllowed:
        rng, cellRef = setRange(columnDict, columnHeader)
        rng.api.FormatConditions.Add(2,3, '=OR(ISNUMBER(SEARCH("<br>",' + cellRef + ')), ISNUMBER(SEARCH("<br/>",' + cellRef + ")))")
        rng.api.FormatConditions(2).Interior.ColorIndex = 6

searchResults = wholeRange.api.Find("~\"")
if searchResults is not None:
    msg_box("There's a double quote in this spreadsheet")
else:
    msg_box("There are no double quotes in this spreadsheet")

# app.api.FindFormat.Clear
# app.api.FindFormat.Interior.ColorIndex = 3
# foundRed = wholeRange.api.Find("*", SearchFormat=True)

# if foundRed is None:
    # msg_box("There are no values exceeding character limits")
# else:
    # msg_box("There are values exceeding character limits")

# app.api.FindFormat.Clear
# app.api.FindFormat.Interior.ColorIndex = 6
# foundYellow = wholeRange.api.Find("*", SearchFormat=True)
# if foundYellow is None:
    # msg_box("There are no break tags in this spreadsheet")
# else:
    # msg_box("There are break tags in this spreadsheet")

2 个答案:

答案 0 :(得分:9)

问题:

查看the github package第22行的可能性:

async def concat_async(async_gen):

这是使用在python 3.5中添加的async关键字,但py2exe仅支持python 3.4。现在jinja看起来是以某种方式扩展python语言(可能在运行时?),以支持早期版本的python中的这个async关键字。 py2exe无法解释此语言扩展名。

修复:

根据the documentation在jinja2版本2.9中添加了异步支持。所以我尝试安装早期版本的jinja(版本2.8),我downloaded here

我通过将%PYTHONHOME%\Lib\site-packages\jinja2的内容移动到其他地方来备份我当前的jinja安装。 提取以前下载的tar.gz文件并通过pip安装包:

cd .\Downloads\dist\Jinja2-2.8 # or wherever you extracted jinja2.8
python setup.py install

作为旁注,我还必须增加递归限制,因为py2exe已达到默认限制。

from distutils.core import setup
import py2exe
import sys
sys.setrecursionlimit(5000)
setup (console=['test.py'])

警告:

如果你使用的是什么,它依赖于最新版本的jinja2,那么在实际运行你的代码时,这可能会失败或产生意想不到的副作用。我正在编写一个非常简单的脚本。

答案 1 :(得分:0)

我在python3.7中编码时遇到了同样的麻烦。我修复了将排除部分添加到我的py2exe文件中的问题:

a = Analysis(['pyinst_test.py'],
         #...
         excludes=['jinja2.asyncsupport','jinja2.asyncfilters'],
         #...)

我从https://github.com/pyinstaller/pyinstaller/issues/2393