6

This command works fine on my personal computer but keeps giving me this error on my work PC. What could be going on? I can run the Char_Limits.py script directly in Powershell without a problem. error: compiling 'C:\ProgramData\Anaconda2\lib\site-packages\jinja2\asyncsupport.py' failed SyntaxError: invalid syntax (asyncsupport.py, line 22)

My setup.py file looks like:

from distutils.core import setup
import py2exe

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

My file looks like:

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")
Hybris
  • 61
  • 1
  • 2

2 Answers2

10

Note:

If you are reading this, I would try Santiago's solution first.

The issue:

Looking at what is likely at line 22 on the github package:

async def concat_async(async_gen):

This is making use of the async keyword which was added in python 3.5, however py2exe only supports up to python 3.4. Now jinja looks to be extending the python language in some way (perhaps during runtime?) to support this async keyword in earlier versions of python. py2exe cannot account for this language extension.

The Fix:

async support was added in jinja2 version 2.9 according to the documentation. So I tried installing an earlier version of jinja (version 2.8) which I downloaded here.

I made a backup of my current jinja installation by moving the contents of %PYTHONHOME%\Lib\site-packages\jinja2 to some other place. extract the previously downloaded tar.gz file and install the package via pip:

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

As a side note, I also had to increase my recursion limit because py2exe was reaching the default limit.

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

Warning:

If whatever it is you are using relies on the latest version of jinja2, then this might fail or have unintended side effects when actually running your code. I was compiling a very simple script.

Aaron
  • 1,893
  • 16
  • 24
  • is there any way to bypass this for Python 2.7? – battery514 Feb 13 '17 at 23:45
  • Find out what you are using that is dependent on asyncsupport.py, and remove this dependency. You could use a tool like this to help you: http://stackoverflow.com/a/30450999/3154314 – Aaron Feb 14 '17 at 15:29
  • Alternatively (if your script is easy enough to convert to python 3 syntax) you could use python 3.5 and use `PyInstaller` to convert it to an executable as suggested here: http://stackoverflow.com/a/33174611/3154314 – Aaron Feb 14 '17 at 16:28
  • I'm sorry, but I am pretty new to Python. asyncsupport.py is part of the jinja2 installation, which is part of pandas (or so I gather). The program I am trying to create a .exe for is using pandas (successfully, I may add). It's just that when I run pyinstaller I get the same type error as py2exe, in that it throws back the asyncsupport error. Is there any way to remove the dependency on that .py file and move on my merry way? If that keyword isn't supported by 2.7, my guess would be I don't need the file any way as pandas works just fine. Thanks again. – battery514 Feb 14 '17 at 17:57
  • I should also mention that, unfortunately, I am unable to upgrade to Python 3.5. I can only go as high as 3.4 as I am using a network server with Windows 2003. – battery514 Feb 14 '17 at 18:04
  • I updated the answer, I was able to repro and 'fix' the issue you were having at least for a 'hello world' app I wrote. – Aaron Feb 14 '17 at 21:42
1

I had the same trouble coding in python3.7. I fixed that adding the excludes part to my py2exe file:

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

I took that from: https://github.com/pyinstaller/pyinstaller/issues/2393

Santiago H
  • 81
  • 1
  • 2
  • In which file would I need to add this code? Sorry, I'm a little new to this. – John918 Apr 26 '21 at 10:41
  • If you need to create a .exe with py2exe module you need to create a different file than your main aplication. You can read this guide: http://www.py2exe.org/index.cgi/Tutorial – Santiago H May 19 '21 at 23:26