Biopython Indentation Error - indentation

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

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!

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!

Related

Getting no module named _internals error while using Bloomberg API

I am currently using Python 2.7 and my OS is Windows 7. While attempting to use the Bloomberg API I am getting this error:
Traceback (most recent call last):
File "datagrab.py", line 1, in <module>
import blpapi, time, json
File "C:\Python27\lib\blpapi\__init__.py", line 5, in <module>
from .internals import CorrelationId
File "C:\Python27\lib\blpapi\internals.py", line 50, in <module>
_internals = swig_import_helper()
File "C:\Python27\lib\blpapi\internals.py", line 42, in swig_import_helper
import _internals
ImportError: No module named _internals
I have set my path variable to point to blpapi3_64.dll and also updated my bloomberg terminal. I have also moved the local blpapi API to a different directory but still the problem exists.
I am kind of new to this API in general. So can someone please guide me?
Thank you in advance!
From your question is sounds like maybe you have tried this, but just outlining one possible solution from the README in the Python Supported Release release available here.
Note that many Python installations add the current directory to the
module search path. If the Python interpreter is invoked from the
installer directory, such a configuration will attempt to use the
(incomplete) local blpapi directory as a module. If the above
import line fails with the message Import Error: No module named
_internals, move to a different directory before invoking python.
I know this question is a bit stale, but in case people end up here like me. Do you have the C++ version of blpapi? it is a requirement for the python api as mentioned here: https://www.bloomberg.com/professional/support/api-library/
so download the C++ zip installer, extract somewhere, and then add it as an environment variable so that the python api can find it:
Environment variable name: BLPAPI_ROOT
Value: C:\blp\blpapi_cpp_3.8.18.1 (THIS IS WHERE MINE IS INSTALLED, YOUR VALUE HERE MAY BE DIFFERENT)
Hope that helps!

Python Django ValueError: source code string cannot contain null bytes

I have put down a Django project I was working on for a couple months, when I attempted to re run the server I got this error:
ValueError: source code string cannot contain null bytes
The Traceback is:
C:\Users\Broja\Desktop\mynewenv\computersite>manage.py runserver
Traceback (most recent call last):
File "C:\Users\Broja\Desktop\mynewenv\computersite\manage.py", line 8, in
<module>
from django.core.management import execute_from_command_line
File "C:\Users\Broja\AppData\Local\Programs\Python\Python35\lib\site-
packages\django\__init__.py", line 3, in <module>
from django.utils.version import get_version
ValueError: source code string cannot contain null bytes
I am not too familiar with the Django Framework so this error confuses me, any help would go a long way. Thanks.
Just convert it to UTF-8. That worked for me :)
Anyone who is looking for a simple solution in windows OS.
Just edit the target file(the file which is causing the issue) in Notepad.
Click File.
Click Save As.
Along the Save Button, there is an option for Encoding, just select
UTF-8 and select All Files option from the Save As Type option and
enjoy

Why pypy is not recognizing my python 2.7/3.4 modules and packages?

I am running my python codes using either python script.py or python3 script.py. (The first one is based on 2.7 and the second one based on 3.4). However, recently I am working on a code that is pretty demanding in terms of computational expense. I am not an expert in python in order to speed up my code (which uses alot of other routines all compatible with both versions of python 2.7 and 3.4). When the .hdf5 input file of my function inside the code is on the order of 1 Mbi, the code is running and producing the result even though slowly. But when the .hdf5 input file of the function is on the order of 1 Gbi, the code almost never proceeds to the end and I am ending up with a "MemoryError:"
Traceback (most recent call last):
File "myscript.py", line 29, in <module>
gizmo.analysis.Image.plot_image(part, 'gas', 'mass.hydrogen.neutral', 'histogram', [0,1],[0,1,2], distance_max, distance_bin_width_Gas, distance_bin_number, part_indices=part_indices, write_plot=write_plot, plot_directory=plot_directory, background_color=background_color, use_column_units=True)
File "/home/username/Desktop/Projects/PaperMaterials/DM_Dominated_Objects/NewFolder2/covering_fractions/Simulations/gizmo/gizmo_analysis.py", line 817, in plot_image
weights = part[spec_name].prop(weight_prop_name, part_indices)
File "/home/username/Desktop/Projects/PaperMaterials/DM_Dominated_Objects/NewFolder2/covering_fractions/Simulations/gizmo/gizmo_io.py", line 145, in prop
self.prop(property_name.replace('mass.', 'massfraction.'), indices))
File "/home/username/Desktop/Projects/PaperMaterials/DM_Dominated_Objects/NewFolder2/covering_fractions/Simulations/gizmo/gizmo_io.py", line 157, in prop
values = (1 - self.prop('massfraction', indices)[:, 0] -
File "/home/username/Desktop/Projects/PaperMaterials/DM_Dominated_Objects/NewFolder2/covering_fractions/Simulations/gizmo/gizmo_io.py", line 65, in prop
return self[property_name][indices]
MemoryError
After doing some search on this website, I noticed that I can run python codes using pypy for example to speed them up. Since I am using Fedora 21 (32 bit) OS which comes with a pypy 2.4 version in its repository, I was able to install this version of pypy successfully. However, when I try to run my code using the command line pypy myscript.py instead of the ones mentioned above, I am left with error message:
File "app_main.py", line 75, in run_toplevel File "myscript.py",
line 1, in
import numpy as np ImportError: No module named numpy
It seems that pypy does not recognize all the python modules and packages including this particular one.
What would be your best suggestion for me given the fact that I would like to stick with Fedora 21 Linux distribution such that all the python modules and packages be recognized by pypy resulting in faster codes?
I can post what myscript.py is but I am not sure if that would be helpful at all given the fact that I am using lots of other routines in it.
PyPy 2.4 is a rather outdated version which contains no support for numpy (even if you tried to install it separately).
In any case it is very unlikely that even a brand-new version of PyPy would reduce the memory usage of a numpy-heavy program.
If you're hitting MemoryError at the scale of ~1-3GB of data in a single process, on a 32-bit OS, then it seems clear to me that you need a 64-bit OS.

Pyinstaller-generated exe cannot run on another computer

My computer's OS is 64-bit win 10. Python 2.7, 32-bit.
My code is plot.py, simple as below:
import matplotlib.pyplot as plt
import FileDialog
plt.plot([1,2,3,4])
plt.ylabel('some numbers')
plt.show()
When I execute pyinstaller -F plot.py, the generated
plot.exe works as expected on my current computer. However, I get an error if I try to run it on a different 32-bit Windows 7 computer:
Traceback (most recent call last):
File "site-packages\GUI_tempCtrl\plot.py", line 3, in <module>
File "lib\site-packages\matplotlib\pyplot.py", line 3147, in plot
File "lib\site-packages\matplotlib\pyplot.py", line 928, in gca
File "lib\site-packages\matplotlib\pyplot.py", line 578, in gcf
File "lib\site-packages\matplotlib\pyplot.py", line 527, in figure
File "lib\site-packages\matplotlib\backends\backend_tkagg.py", line 84, in new
_figure_manager
File "lib\site-packages\matplotlib\backends\backend_tkagg.py", line 92, in new
_figure_manager_given_figure
File "lib\lib-tk\Tkinter.py", line 1814, in __init__
_tkinter.TclError: Can't find a usable init.tcl in the following directories:
C:/Users/ADMINI~1/AppData/Local/Temp/lib/tcl8.5 C:/Users/Administrator/lib/t
cl8.5 C:/Users/lib/tcl8.5 C:/Users/Administrator/library C:/Users/library C:/Use
rs/tcl8.5.15/library C:/tcl8.5.15/library
This probably means that Tcl wasn't installed properly.
Failed to execute script plot
You can try downgrading pyinstaller to v3.1 and rebuilding your exe file on your first computer to solve this issue.
Alternatively, you can fix it on your second computer by setting the TCL_LIBRARY environment variable to C:\Python27\tcl\tcl8.5\ (or wherever tcl8.5 is located)
PyInstaller has a known issue with Tkinter applications on Windows 7 and Windows XP.
Since this issue has gone unfixed for two years, I've gone ahead and started a bounty. Until the issue is fixed, there are a few workarounds you can try:
Workaround 1 - Manually copy missing files
As mentioned in a related issue, you can manually copy the missing files from your local Python installation.
Find your local Python installation. (%LocalAppData%\Programs\Python)
Make a copy of the missing folder (...\Python36-32\tcl\<missing_folder>)
Move the copy to your application's tcl folder (...\dist\<app_name>\tcl\<missing_folder>)
Workaround 2 - Run with --onefile
Running PyInstaller in --onefile mode seems to avoid this issue.
However, note that running in single file mode will increase startup time.
Workaround 3 - Downgrade to PyInstaller 3.1.0
pip install pyinstaller==3.1.0
According to ugoertz, downgrading to PyInstaller 3.1.0 resolved the issue.
Downgrading to 3.1.0 (and also downgrading setuptools to 19.2 because of the problem described in #1941) fixed the issue for me.

Python importerror : pyqtconfig

I am trying to install and run Eric4. I get the following error message when i try to open Eric after installation. Could anyone please let me know how to proceed?
--------------------------------------------------------------------------------
2013-07-31, 15:18:59
--------------------------------------------------------------------------------
<type 'exceptions.ImportError'>:
cannot import name pyqtconfig
--------------------------------------------------------------------------------
File "C:\Python27\Lib\site-packages\eric4\eric4.py", line 295, in <module>
main()
File "C:\Python27\Lib\site-packages\eric4\eric4.py", line 207, in main
Startup.setLibraryPaths()
File "C:\Python27\Lib\site-packages\eric4\Utilities\Startup.py", line 195, in setLibraryPaths
from PyQt4 import pyqtconfig
--------------------------------------------------------------------------------
Version Numbers:
Python 2.7
Qt 4.8.4
PyQt4 4.10.2
sip 4.14.7
QScintilla 2.7.2
eric4 4.5.8 (r4410)
Platform: win32
2.7 (r27:82525, Jul 4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)]
Thanks a lot
Sorry for my poor English. After some googling I found that in Pyqt 4.10 and above, pyqtconfig.py seems be removed. Please check http://pyqt.sourceforge.net/Docs/PyQt4/build_system.html#module-PyQt4.pyqtconfig
I added a pyqtconfig.py and sipconfig.py get from web.
After that Eric4 starts (with some errors). Haven't do many operations in the editor yet.