Python Forum
code works in python shell, but not in a terminal
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
code works in python shell, but not in a terminal
#1
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?
Reply
#2
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
Reply
#3
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)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
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.
Reply
#5
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.
Reply
#6
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  My code works on Jupyter Lab/Notebook, but NOT on Visual Code Editor jst 4 872 Nov-15-2023, 06:56 PM
Last Post: jst
  Help creating shell scrip for python file marciokoko 10 1,254 Sep-16-2023, 09:46 PM
Last Post: snippsat
  invoking python in Terminal Euler 2 591 Aug-25-2023, 06:17 AM
Last Post: perfringo
  Code works but doesn't give the right results colin_dent 2 674 Jun-22-2023, 06:04 PM
Last Post: jefsummers
  Launch Python IDLE Shell from terminal Pavel_47 5 1,143 Feb-17-2023, 02:53 PM
Last Post: Pavel_47
  Python VS Code: using print command twice but not getting output from terminal kdx264 4 1,033 Jan-16-2023, 07:38 PM
Last Post: Skaperen
  Code used to work 100%, now sometimes works! muzicman0 5 1,380 Jan-13-2023, 05:09 PM
Last Post: muzicman0
  Can a program execute code in iPython shell and get result? deanhystad 3 1,660 Jun-17-2022, 03:45 AM
Last Post: Larz60+
  batch file for running python scipt in Windows shell MaartenRo 2 1,824 Jan-21-2022, 02:36 PM
Last Post: MaartenRo
  Pyspark - my code works but I want to make it better Kevin 1 1,745 Dec-01-2021, 05:04 AM
Last Post: Kevin

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020