Python Forum

Full Version: code works in python shell, but not in a terminal
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a simple program to put passport size photos next to the correct name in an excel table.

In the python shell, everything works and it does just what I want.

I use Ubuntu 16.04. When I start a terminal and run the program, I get the following error: (the ^ does not display properly on this web page, in a terminal it is directly under the :)

Quote:pedro@pedro-dodgySSD:~/myPython$ ./putPicsinColumn1version4.py
File "./putPicsinColumn1version4.py", line 75
for sheet in sheets:
^
SyntaxError: invalid syntax
pedro@pedro-dodgySSD:~/myPython$

This seems to tell me the colon is wrong, but I'm certain it is needed in Python.

Line 75 is the first line below, the code is:

for sheet in sheets:
      wb.active = wb[sheet]
      print('Active Sheet is ' + str(wb.active))
      print('Max row is ' + str(wb.active.max_row))
      for rowNum in range(beginRow, wb.active.max_row + 1):
          print('resizing row height in ' + str(wb.active))
          wb.active.row_dimensions[rowNum].height = 72 # about 1"
As I said, this works when I test it in the idle shell, just not when I want to run it in terminal. Anyone know why? Anyone had this kind of problem?
You may have mixed space characters with hard tab characters in the indentation. Make sure your editor is configured to enter 4 space characters when you hit the tab space.

You can reindent the code with the reindent command, to do this in a terminal do
Output:
python -m pip install reindent reindent putPicsinColumn1version4.py
then reopen the file. The original version of the file is kept as putPicsinColumn1versio4.py.bak
in the example you show here lines 2-5 are indented 5 spaces, I guess you use 4 spaces by default, like at lines 6-7
also check the line just before the line where the error is shown (75), maybe it's there (e.g. missing bracket)
Well the idle3 editor is set to put 4 spaces for a tab.

The bit of my little program that does the work was very difficult to indent. In the end, I tried this, which works every time in the idle3 python shell.

for sheet in sheets:
	activeDirectory = pathToPhotos + sheet + '/'
	print('the active photo directory is ' + activeDirectory)
	for filename in os.listdir(activeDirectory):
			if not (filename.endswith('.jpg')):
				continue
			for rowNum in range(beginRow, wb[sheet].max_row + 1):
				getName = filename.split('.')
				if (getName[0] == wb[sheet].cell(row=rowNum, column=col).value):
					print('found the name! Name is: %s ' % (getName[0]))
					print('Putting pic in row ' + str(rowNum))
					img = Image(activeDirectory + filename)
					wb[sheet].add_image(img, 'A' + str(rowNum))
Seems like the indents are all over the place, but it works each time in the python shell. However, in a bash terminal, it never gets that far, because of the above mentioned problem with the colon.
We'll try to see if there is an invisible hidden character in the source file. Try this in a python shell,
in a directory containing the script
>>> for line in open('putPicsinColumn1version4.py'):
...     print(repr(line))
...
Then look around 'for sheet in sheets' in the output to see if there is something special there. Hidden characters may appear when someone copies and pastes a section of code from a pdf file into a code editor for example.
Thank you, I tried that but there was no hidden character.

Turns out, I had missed a bracket in the line 70 print('Resizing the row heights for row %s to max_row...' % (startRow))

I forgot the last bracket. After that, the routine almost worked, except in line 60 I needed to put str(startRow) because startRow is an integer.

Now it works, and I have learned about the power of brackets!!

Thanks for your good advice!