1

I've been dabbling in biopython for about a year and I recently upgraded to Biopython release 1.59. I've been refreshing my skills with some tutorials but I always get the error below when I run a for loop and any module from the biopython library:

    IndentationError: expected an indented block

I only get this error when I call the .py file written in Komodo Edit version 7.0.2 from the command line terminal:

    Priyas-iMac:~ Priya$ python /Users/priya/Documents/Python/Tutorials/BioParse.py
    Traceback (most recent call last):
      File "/Users/priya/Documents/Python/Tutorials/BioParse.py", line 4, in <module>
        SeqIO.write(rc, "/Users/priya/Documents/Python/Tutorials/ls_orchid_rc.fasta", "fasta")
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Bio/SeqIO/__init__.py", line 400, in write
        from Bio import AlignIO
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Bio/AlignIO/__init__.py", line 147, in <module>
        import NexusIO
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Bio/AlignIO/NexusIO.py", line 19, in <module>
        from Bio.Nexus import Nexus
      File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Bio/Nexus/Nexus.py", line 15, in <module>
        import os,sys, math, random, copy
      File "/Users/Priya/Documents/Python/Tutorials/random.py", line 27
        randLowHigh(5,10)
        ^
    IndentationError: expected an indented block

When I use the command line to call old .py files that I wrote a year ago as above they run fine. And when I start up python directly and type in the tutorial example line by line it works fine:

    Priyas-iMac:~ Priya$ python 
    Python 2.7.3 (default, Apr 19 2012, 00:55:09) 
    [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from Bio import SeqIO
    >>> for seq_record in SeqIO.parse("/Users/priya/Documents/Python/Tutorials/ls_orchid.txt","fasta"):
    ...      print seq_record.id
    ... 
    gi|2765658|emb|Z78533.1|CIZ78533
    gi|2765657|emb|Z78532.1|CCZ78532

How can I fix my .py file so I can just run it from the terminal?

Any insight into this problem would be greatly appreciated!

Priya

priyasshah
  • 63
  • 1
  • 6

2 Answers2

2

It turns out that when I was originally learning python, I stupidly named a tutorial file "random.py" to practice making a random number generator. When biopython was importing other required modules, it kept pointing to that file instead of the random library module:

  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/Bio/Nexus/Nexus.py", line 15, in <module>
    import os,sys, math, random, copy
  File "/Users/Priya/Documents/Python/Tutorials/random.py", line 27
    randLowHigh(5,10)

Thanks for the help, everyone!

priyasshah
  • 63
  • 1
  • 6
1

Check if all the files and modules imported have the same indentation (tabs or spaces and same number of spaces).

From the Style guide for Python code you can read:

Use 4 spaces per indentation level.

For really old code that you don't want to mess up, you can continue to use 8-space tabs.

And regarding tabs and spaces:

Never mix tabs and spaces.

The most popular way of indenting Python is with spaces only. The second-most popular way is with tabs only. Code indented with a mixture of tabs and spaces should be converted to using spaces exclusively. When invoking the Python command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. These options are highly recommended!

For new projects, spaces-only are strongly recommended over tabs. Most editors have features that make this easy to do.

Maybe Komodo is changing something automatically (some editors do). Try to edit the file in a more plain editor and look for the tabs. You may have an option to show you the spaces and tabs as visible.

Hope it helps!

Community
  • 1
  • 1
viridis
  • 177
  • 10
  • Thanks for the suggestions. I tried replacing the tab with four spaces in Komodo Edit and it gives me the same error. I also tried typing it into Text Editor (on a Mac) fresh and get the same error. I will try and read some more about this today and see if Komodo Edit is doing something weird. Thanks again! – priyasshah May 04 '12 at 15:41
  • Have a look: http://stackoverflow.com/questions/1024435/howto-fix-python-indentation – viridis May 04 '12 at 21:29