Python 3 compatibility syntax error fixes (#10519)

* Use Python 3 style print function

* Use 'Exception as e' instead of 'Exception, e'

* Unpack tuple arguments explicitly in instead of relying on auto unpacking

* Use consistent indentation

* Use 0 if stock_frozen_upto_days is None
diff --git a/erpnext/accounts/doctype/account/chart_of_accounts/import_from_openerp.py b/erpnext/accounts/doctype/account/chart_of_accounts/import_from_openerp.py
index cb95bd1..9e3388b 100644
--- a/erpnext/accounts/doctype/account/chart_of_accounts/import_from_openerp.py
+++ b/erpnext/accounts/doctype/account/chart_of_accounts/import_from_openerp.py
@@ -4,7 +4,7 @@
 """
 Import chart of accounts from OpenERP sources
 """
-from __future__ import unicode_literals
+from __future__ import print_function, unicode_literals
 
 import os, json
 import ast
@@ -229,7 +229,7 @@
 
 		filename = src["id"][5:] + "_" + chart_id
 
-		print "building " + filename
+		print("building " + filename)
 		chart = {}
 		chart["name"] = src["name"]
 		chart["country_code"] = src["id"][5:]
diff --git a/erpnext/accounts/doctype/payment_entry/payment_entry.py b/erpnext/accounts/doctype/payment_entry/payment_entry.py
index 7bb9a52..9832c05 100644
--- a/erpnext/accounts/doctype/payment_entry/payment_entry.py
+++ b/erpnext/accounts/doctype/payment_entry/payment_entry.py
@@ -393,7 +393,7 @@
 			if self.payment_type=="Receive":
 				against_account = self.paid_to
 			else:
-				 against_account = self.paid_from
+				against_account = self.paid_from
 
 
 			party_gl_dict = self.get_gl_dict({
diff --git a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
index 96d617e..780edd8 100644
--- a/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
+++ b/erpnext/accounts/doctype/sales_invoice/sales_invoice.py
@@ -109,7 +109,7 @@
 
 		if not self.recurring_id:
 			frappe.get_doc('Authorization Control').validate_approving_authority(self.doctype,
-			 	self.company, self.base_grand_total, self)
+				self.company, self.base_grand_total, self)
 
 		self.check_prev_docstatus()
 
diff --git a/erpnext/accounts/doctype/shipping_rule/shipping_rule.py b/erpnext/accounts/doctype/shipping_rule/shipping_rule.py
index 7faaf11..a47df2d 100644
--- a/erpnext/accounts/doctype/shipping_rule/shipping_rule.py
+++ b/erpnext/accounts/doctype/shipping_rule/shipping_rule.py
@@ -54,13 +54,15 @@
 			d.idx = i + 1
 
 	def validate_overlapping_shipping_rule_conditions(self):
-		def overlap_exists_between((x1, x2), (y1, y2)):
+		def overlap_exists_between(num_range1, num_range2):
 			"""
-				(x1, x2) and (y1, y2) are two ranges
-				if condition x = 100 to 300
-				then condition y can only be like 50 to 99 or 301 to 400
+				num_range1 and num_range2 are two ranges
+				ranges are represented as a tuple e.g. range 100 to 300 is represented as (100, 300)
+				if condition num_range1 = 100 to 300
+				then condition num_range2 can only be like 50 to 99 or 301 to 400
 				hence, non-overlapping condition = (x1 <= x2 < y1 <= y2) or (y1 <= y2 < x1 <= x2)
 			"""
+			(x1, x2), (y1, y2) = num_range1, num_range2
 			separate = (x1 <= x2 <= y1 <= y2) or (y1 <= y2 <= x1 <= x2)
 			return (not separate)
 
diff --git a/erpnext/controllers/website_list_for_contact.py b/erpnext/controllers/website_list_for_contact.py
index 73badc2..65360ec 100644
--- a/erpnext/controllers/website_list_for_contact.py
+++ b/erpnext/controllers/website_list_for_contact.py
@@ -68,8 +68,8 @@
 	if txt:
 		if meta.get_field('items'):
 			if meta.get_field('items').options:
-			   child_doctype = meta.get_field('items').options
-			   for item in frappe.get_all(child_doctype, {"item_name": ['like', "%" + txt + "%"]}):
+				child_doctype = meta.get_field('items').options
+				for item in frappe.get_all(child_doctype, {"item_name": ['like', "%" + txt + "%"]}):
 					child = frappe.get_doc(child_doctype, item.name)
 					or_filters.append([doctype, "name", "=", child.parent])
 
diff --git a/erpnext/demo/setup/setup_data.py b/erpnext/demo/setup/setup_data.py
index ae792ac..cec425c 100644
--- a/erpnext/demo/setup/setup_data.py
+++ b/erpnext/demo/setup/setup_data.py
@@ -1,4 +1,4 @@
-from __future__ import unicode_literals
+from __future__ import print_function, unicode_literals
 
 import random, json
 import frappe, erpnext
@@ -42,7 +42,7 @@
 	frappe.clear_cache()
 
 def complete_setup(domain='Manufacturing'):
-	print "Complete Setup..."
+	print("Complete Setup...")
 	from frappe.desk.page.setup_wizard.setup_wizard import setup_complete
 
 	if not frappe.get_all('Company', limit=1):
diff --git a/erpnext/demo/user/stock.py b/erpnext/demo/user/stock.py
index 1b12db8..43668fe 100644
--- a/erpnext/demo/user/stock.py
+++ b/erpnext/demo/user/stock.py
@@ -1,7 +1,7 @@
 # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
 # License: GNU General Public License v3. See license.txt
 
-from __future__ import unicode_literals
+from __future__ import print_function, unicode_literals
 
 import frappe, random
 from frappe.desk import query_report
@@ -36,7 +36,7 @@
 			try:
 				pr.submit()
 			except NegativeStockError:
-				print 'Negative stock for {0}'.format(po)
+				print('Negative stock for {0}'.format(po))
 				pass
 			frappe.db.commit()
 
diff --git a/erpnext/hr/doctype/leave_block_list/test_leave_block_list.py b/erpnext/hr/doctype/leave_block_list/test_leave_block_list.py
index 42b26e3..210b8b7 100644
--- a/erpnext/hr/doctype/leave_block_list/test_leave_block_list.py
+++ b/erpnext/hr/doctype/leave_block_list/test_leave_block_list.py
@@ -10,7 +10,7 @@
 
 class TestLeaveBlockList(unittest.TestCase):
 	def tearDown(self):
-		 frappe.set_user("Administrator")
+		frappe.set_user("Administrator")
 
 	def test_get_applicable_block_dates(self):
 		frappe.set_user("test@example.com")
diff --git a/erpnext/manufacturing/doctype/bom/bom.py b/erpnext/manufacturing/doctype/bom/bom.py
index 71ff43f..e48a002 100644
--- a/erpnext/manufacturing/doctype/bom/bom.py
+++ b/erpnext/manufacturing/doctype/bom/bom.py
@@ -259,15 +259,15 @@
 
 			
 	def update_stock_qty(self):
- 		for m in self.get('items'):
+		for m in self.get('items'):
 		
 			if not m.conversion_factor:
 				m.conversion_factor = flt(get_conversion_factor(m.item_code, m.uom)['conversion_factor'])
- 			if m.uom and m.qty:
- 				m.stock_qty = flt(m.conversion_factor)*flt(m.qty)
- 			if not m.uom and m.stock_uom:
- 				m.uom = m.stock_uom
- 				m.qty = m.stock_qty
+			if m.uom and m.qty:
+				m.stock_qty = flt(m.conversion_factor)*flt(m.qty)
+			if not m.uom and m.stock_uom:
+				m.uom = m.stock_uom
+				m.qty = m.stock_qty
  
  
 	def set_conversion_rate(self):
diff --git a/erpnext/patches/v4_0/new_address_template.py b/erpnext/patches/v4_0/new_address_template.py
index f644a5a..fa66027 100644
--- a/erpnext/patches/v4_0/new_address_template.py
+++ b/erpnext/patches/v4_0/new_address_template.py
@@ -1,4 +1,4 @@
-from __future__ import unicode_literals
+from __future__ import print_function, unicode_literals
 import frappe
 
 def execute():
@@ -10,5 +10,5 @@
 				frappe.db.get_value("Global Defaults", "Global Defaults", "country")})
 			d.insert()
 		except:
-			print frappe.get_traceback()
+			print(frappe.get_traceback())
 
diff --git a/erpnext/patches/v4_0/reset_permissions_for_masters.py b/erpnext/patches/v4_0/reset_permissions_for_masters.py
index b2f1fcd..bc1b438 100644
--- a/erpnext/patches/v4_0/reset_permissions_for_masters.py
+++ b/erpnext/patches/v4_0/reset_permissions_for_masters.py
@@ -1,7 +1,7 @@
 # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
 # License: GNU General Public License v3. See license.txt
 
-from __future__ import unicode_literals
+from __future__ import print_function, unicode_literals
 from frappe.permissions import reset_perms
 
 def execute():
@@ -16,5 +16,5 @@
 		try:
 			reset_perms(doctype)
 		except:
-			print "Error resetting perms for", doctype
+			print("Error resetting perms for", doctype)
 			raise
diff --git a/erpnext/patches/v4_0/set_naming_series_property_setter.py b/erpnext/patches/v4_0/set_naming_series_property_setter.py
index 9d12f14..e61a596 100644
--- a/erpnext/patches/v4_0/set_naming_series_property_setter.py
+++ b/erpnext/patches/v4_0/set_naming_series_property_setter.py
@@ -1,7 +1,7 @@
 # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
 # License: GNU General Public License v3. See license.txt
 
-from __future__ import unicode_literals
+from __future__ import print_function, unicode_literals
 
 import frappe
 from frappe.custom.doctype.property_setter.property_setter import make_property_setter
@@ -91,7 +91,7 @@
 		(new_series, new_series))
 
 	if not (default_series and default_series[0][0]):
-		print "[Skipping] Cannot guess which naming series to use for", doctype
+		print("[Skipping] Cannot guess which naming series to use for", doctype)
 		return
 
 	return default_series[0][0]
diff --git a/erpnext/patches/v4_0/split_email_settings.py b/erpnext/patches/v4_0/split_email_settings.py
index 21dc050..5d1dea6 100644
--- a/erpnext/patches/v4_0/split_email_settings.py
+++ b/erpnext/patches/v4_0/split_email_settings.py
@@ -1,11 +1,11 @@
 # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
 # License: GNU General Public License v3. See license.txt
 
-from __future__ import unicode_literals
+from __future__ import print_function, unicode_literals
 import frappe
 
 def execute():
-	print "WARNING!!!! Email Settings not migrated. Please setup your email again."
+	print("WARNING!!!! Email Settings not migrated. Please setup your email again.")
 
 	# this will happen if you are migrating very old accounts
 	# comment out this line below and remember to create new Email Accounts
diff --git a/erpnext/patches/v4_0/update_account_root_type.py b/erpnext/patches/v4_0/update_account_root_type.py
index e3edee9..15ddf03 100644
--- a/erpnext/patches/v4_0/update_account_root_type.py
+++ b/erpnext/patches/v4_0/update_account_root_type.py
@@ -1,7 +1,7 @@
 # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
 # License: GNU General Public License v3. See license.txt
 
-from __future__ import unicode_literals
+from __future__ import print_function, unicode_literals
 import frappe
 
 def execute():
@@ -31,4 +31,4 @@
 				frappe.db.sql("""UPDATE tabAccount SET root_type=%s WHERE lft>%s and rgt<%s""",
 					(root.root_type, root.lft, root.rgt))
 			else:
-				print b"Root type not found for {0}".format(root.name.encode("utf-8"))
+				print(b"Root type not found for {0}".format(root.name.encode("utf-8")))
diff --git a/erpnext/patches/v4_2/fix_gl_entries_for_stock_transactions.py b/erpnext/patches/v4_2/fix_gl_entries_for_stock_transactions.py
index 0df5801..16932af 100644
--- a/erpnext/patches/v4_2/fix_gl_entries_for_stock_transactions.py
+++ b/erpnext/patches/v4_2/fix_gl_entries_for_stock_transactions.py
@@ -1,7 +1,7 @@
 # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
 # License: GNU General Public License v3. See license.txt
 
-from __future__ import unicode_literals
+from __future__ import print_function, unicode_literals
 import frappe
 from frappe.utils import flt
 
@@ -37,7 +37,7 @@
 
 			if stock_bal and account_bal and abs(flt(stock_bal[0][0]) - flt(account_bal[0][0])) > 0.1:
 				try:
-					print voucher_type, voucher_no, stock_bal[0][0], account_bal[0][0]
+					print(voucher_type, voucher_no, stock_bal[0][0], account_bal[0][0])
 
 					frappe.db.sql("""delete from `tabGL Entry`
 						where voucher_type=%s and voucher_no=%s""", (voucher_type, voucher_no))
@@ -45,10 +45,10 @@
 					voucher = frappe.get_doc(voucher_type, voucher_no)
 					voucher.make_gl_entries(repost_future_gle=False)
 					frappe.db.commit()
-				except Exception, e:
-					print frappe.get_traceback()
+				except Exception as e:
+					print(frappe.get_traceback())
 					rejected.append([voucher_type, voucher_no])
 					frappe.db.rollback()
 
-		print "Failed to repost: "
-		print rejected
+		print("Failed to repost: ")
+		print(rejected)
diff --git a/erpnext/patches/v4_2/party_model.py b/erpnext/patches/v4_2/party_model.py
index 8f4fc33..6f93352 100644
--- a/erpnext/patches/v4_2/party_model.py
+++ b/erpnext/patches/v4_2/party_model.py
@@ -1,7 +1,7 @@
 # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
 # License: GNU General Public License v3. See license.txt
 
-from __future__ import unicode_literals
+from __future__ import print_function, unicode_literals
 import frappe
 
 def execute():
@@ -109,7 +109,7 @@
 		and exists(select gle.name from `tabGL Entry` gle where gle.account = tabAccount.name)""")
 		
 	if accounts_not_deleted:
-		print "Accounts not deleted: " + "\n".join(accounts_not_deleted)
+		print("Accounts not deleted: " + "\n".join(accounts_not_deleted))
 		
 
 def remove_customer_supplier_account_report():
diff --git a/erpnext/patches/v4_2/repost_sle_for_si_with_no_warehouse.py b/erpnext/patches/v4_2/repost_sle_for_si_with_no_warehouse.py
index 44bec00..1356129 100644
--- a/erpnext/patches/v4_2/repost_sle_for_si_with_no_warehouse.py
+++ b/erpnext/patches/v4_2/repost_sle_for_si_with_no_warehouse.py
@@ -1,7 +1,7 @@
 # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
 # License: GNU General Public License v3. See license.txt
 
-from __future__ import unicode_literals
+from __future__ import print_function, unicode_literals
 import frappe
 from erpnext.stock.stock_ledger import NegativeStockError
 
@@ -28,7 +28,7 @@
 			frappe.local.stockledger_exceptions = None
 			frappe.db.rollback()
 
-	print "Failed to repost: ", failed_list
+	print("Failed to repost: ", failed_list)
 					
 		
 	
\ No newline at end of file
diff --git a/erpnext/patches/v4_2/set_company_country.py b/erpnext/patches/v4_2/set_company_country.py
index 929f6c5..89f07f2 100644
--- a/erpnext/patches/v4_2/set_company_country.py
+++ b/erpnext/patches/v4_2/set_company_country.py
@@ -1,13 +1,13 @@
 # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
 # License: GNU General Public License v3. See license.txt
 
-from __future__ import unicode_literals
+from __future__ import print_function, unicode_literals
 import frappe
 
 def execute():
 	country = frappe.db.get_single_value("Global Defaults", "country")
 	if not country:
-		print "Country not specified in Global Defaults"
+		print("Country not specified in Global Defaults")
 		return
 
 	for company in frappe.db.sql_list("""select name from `tabCompany`
diff --git a/erpnext/patches/v5_0/repost_gle_for_jv_with_multiple_party.py b/erpnext/patches/v5_0/repost_gle_for_jv_with_multiple_party.py
index da58ae2..76efdcc 100644
--- a/erpnext/patches/v5_0/repost_gle_for_jv_with_multiple_party.py
+++ b/erpnext/patches/v5_0/repost_gle_for_jv_with_multiple_party.py
@@ -1,7 +1,7 @@
 # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
 # License: GNU General Public License v3. See license.txt
 
-from __future__ import unicode_literals
+from __future__ import print_function, unicode_literals
 import frappe
 
 def execute():
@@ -21,6 +21,6 @@
 		je.make_gl_entries()
 		
 	if je_list:
-		print je_list
+		print(je_list)
 		
 		
\ No newline at end of file
diff --git a/erpnext/patches/v5_4/fix_missing_item_images.py b/erpnext/patches/v5_4/fix_missing_item_images.py
index 1891d2d..c0a2513 100644
--- a/erpnext/patches/v5_4/fix_missing_item_images.py
+++ b/erpnext/patches/v5_4/fix_missing_item_images.py
@@ -1,4 +1,4 @@
-from __future__ import unicode_literals
+from __future__ import print_function, unicode_literals
 import frappe
 import os
 from frappe.utils import get_files_path
@@ -45,7 +45,7 @@
 		try:
 			file_data.save()
 		except IOError:
-			print "File {0} does not exist".format(new_file_url)
+			print("File {0} does not exist".format(new_file_url))
 
 			# marking fix to prevent further errors
 			fixed_files.append(file_url)
diff --git a/erpnext/patches/v5_4/notify_system_managers_regarding_wrong_tax_calculation.py b/erpnext/patches/v5_4/notify_system_managers_regarding_wrong_tax_calculation.py
index 125b84f..ba31122 100644
--- a/erpnext/patches/v5_4/notify_system_managers_regarding_wrong_tax_calculation.py
+++ b/erpnext/patches/v5_4/notify_system_managers_regarding_wrong_tax_calculation.py
@@ -1,7 +1,7 @@
 # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
 # License: GNU General Public License v3. See license.txt
 
-from __future__ import unicode_literals
+from __future__ import print_function, unicode_literals
 import frappe
 from frappe.email import sendmail_to_system_managers
 from frappe.utils import get_link_to_form
@@ -36,6 +36,6 @@
 		except:
 			pass
 		
-		print "="*50
-		print content
-		print "="*50
\ No newline at end of file
+		print("="*50)
+		print(content)
+		print("="*50)
\ No newline at end of file
diff --git a/erpnext/patches/v5_7/item_template_attributes.py b/erpnext/patches/v5_7/item_template_attributes.py
index 9f141b5..22b15d3 100644
--- a/erpnext/patches/v5_7/item_template_attributes.py
+++ b/erpnext/patches/v5_7/item_template_attributes.py
@@ -1,7 +1,7 @@
 # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
 # License: GNU General Public License v3. See license.txt
 
-from __future__ import unicode_literals
+from __future__ import print_function, unicode_literals
 import frappe
 import MySQLdb
 
@@ -32,7 +32,7 @@
 			migrate_item_variants()
 
 		except MySQLdb.ProgrammingError:
-			print "`tabItem Variant` not found"
+			print("`tabItem Variant` not found")
 
 def rename_and_reload_doctypes():
 	if "tabVariant Attribute" in frappe.db.get_tables():
diff --git a/erpnext/patches/v6_12/repost_entries_with_target_warehouse.py b/erpnext/patches/v6_12/repost_entries_with_target_warehouse.py
index dc0df0f..fb5eab4 100644
--- a/erpnext/patches/v6_12/repost_entries_with_target_warehouse.py
+++ b/erpnext/patches/v6_12/repost_entries_with_target_warehouse.py
@@ -1,7 +1,7 @@
 # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
 # License: GNU General Public License v3. See license.txt
 
-from __future__ import unicode_literals
+from __future__ import print_function, unicode_literals
 import frappe
 
 """
@@ -38,19 +38,19 @@
 	si_list = get_affected_sales_invoice()
 	
 	if so_list or dn_list or si_list:
-		print "Entries with Target Warehouse:"
+		print("Entries with Target Warehouse:")
 		
 		if so_list:
-			print "Sales Order"
-			print so_list
+			print("Sales Order")
+			print(so_list)
 
 		if dn_list:
-			print "Delivery Notes"
-			print [d.name for d in dn_list]
+			print("Delivery Notes")
+			print([d.name for d in dn_list])
 
 		if si_list:
-			print "Sales Invoice"
-			print [d.name for d in si_list]
+			print("Sales Invoice")
+			print([d.name for d in si_list])
 		
 		
 def repost():
@@ -61,34 +61,34 @@
 	frappe.db.commit()
 	
 	if dn_failed_list:
-		print "-"*40
-		print "Delivery Note Failed to Repost"
-		print dn_failed_list
+		print("-"*40)
+		print("Delivery Note Failed to Repost")
+		print(dn_failed_list)
 
 	if si_failed_list:
-		print "-"*40
-		print "Sales Invoice Failed to Repost"
-		print si_failed_list
-		print 
+		print("-"*40)
+		print("Sales Invoice Failed to Repost")
+		print(si_failed_list)
+		print()
 		
-		print """
+		print("""
 If above Delivery Notes / Sales Invoice failed due to negative stock, follow these steps:
 	- Ensure that stock is available for those items in the mentioned warehouse on the date mentioned in the error
 	- Run this patch again
-"""
+""")
 	
 def repost_dn(dn_failed_list):
 	dn_list = get_affected_delivery_notes()
 		
 	if dn_list:
-		print "-"*40
-		print "Reposting Delivery Notes"
+		print("-"*40)
+		print("Reposting Delivery Notes")
 
 	for dn in dn_list:
 		if dn.docstatus == 0:
 			continue
 			
-		print dn.name
+		print(dn.name)
 	
 		try:
 			dn_doc = frappe.get_doc("Delivery Note", dn.name)
@@ -107,7 +107,7 @@
 		except Exception:
 			dn_failed_list.append(dn.name)
 			frappe.local.stockledger_exceptions = None
-			print frappe.get_traceback()
+			print(frappe.get_traceback())
 			frappe.db.rollback()
 		
 	frappe.db.sql("update `tabDelivery Note Item` set target_warehouse='' where docstatus=0")
@@ -116,14 +116,14 @@
 	si_list = get_affected_sales_invoice()
 
 	if si_list:
-		print "-"*40
-		print "Reposting Sales Invoice"
+		print("-"*40)
+		print("Reposting Sales Invoice")
 	
 	for si in si_list:
 		if si.docstatus == 0:
 			continue
 		
-		print si.name
+		print(si.name)
 	
 		try:
 			si_doc = frappe.get_doc("Sales Invoice", si.name)
@@ -141,7 +141,7 @@
 		except Exception:
 			si_failed_list.append(si.name)
 			frappe.local.stockledger_exceptions = None
-			print frappe.get_traceback()
+			print(frappe.get_traceback())
 			frappe.db.rollback()
 		
 	frappe.db.sql("update `tabSales Invoice Item` set target_warehouse='' where docstatus=0")
@@ -152,8 +152,8 @@
 	frappe.db.sql("update `tabSales Order Item` set target_warehouse=''")
 	
 	if so_list:
-		print "-"*40
-		print "Sales Order reposted"
+		print("-"*40)
+		print("Sales Order reposted")
 	
 	
 def get_affected_delivery_notes():
diff --git a/erpnext/patches/v6_4/fix_expense_included_in_valuation.py b/erpnext/patches/v6_4/fix_expense_included_in_valuation.py
index 436dd02..7ed15ab 100644
--- a/erpnext/patches/v6_4/fix_expense_included_in_valuation.py
+++ b/erpnext/patches/v6_4/fix_expense_included_in_valuation.py
@@ -1,7 +1,7 @@
 # Copyright (c) 2013, Frappe Technologies Pvt. Ltd. and Contributors
 # License: GNU General Public License v3. See license.txt
 
-from __future__ import unicode_literals
+from __future__ import print_function, unicode_literals
 import frappe
 from frappe.utils import cstr
 
@@ -33,7 +33,7 @@
 				(pi.name, company.expenses_included_in_valuation))
 
 			if gle_for_expenses_included_in_valuation:
-				print pi.name
+				print(pi.name)
 
 				frappe.db.sql("""delete from `tabGL Entry`
 					where voucher_type='Purchase Invoice' and voucher_no=%s""", pi.name)
diff --git a/erpnext/patches/v6_4/fix_journal_entries_due_to_reconciliation.py b/erpnext/patches/v6_4/fix_journal_entries_due_to_reconciliation.py
index b1464d5..b53412d 100644
--- a/erpnext/patches/v6_4/fix_journal_entries_due_to_reconciliation.py
+++ b/erpnext/patches/v6_4/fix_journal_entries_due_to_reconciliation.py
@@ -1,7 +1,7 @@
 # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
 # License: GNU General Public License v3. See license.txt
 
-from __future__ import unicode_literals
+from __future__ import print_function, unicode_literals
 import frappe
 
 def execute():
@@ -44,7 +44,7 @@
 				where name=%s""", d.name)
 
 	for d in journal_entries:
-		print d
+		print(d)
 		# delete existing gle
 		frappe.db.sql("delete from `tabGL Entry` where voucher_type='Journal Entry' and voucher_no=%s", d)
 
diff --git a/erpnext/patches/v6_4/make_image_thumbnail.py b/erpnext/patches/v6_4/make_image_thumbnail.py
index 702148a..3315acc 100644
--- a/erpnext/patches/v6_4/make_image_thumbnail.py
+++ b/erpnext/patches/v6_4/make_image_thumbnail.py
@@ -1,3 +1,4 @@
+from __future__ import print_function
 import frappe
 
 def execute():
@@ -11,4 +12,4 @@
 				if item_doc.thumbnail:
 					item_doc.db_set("thumbnail", item_doc.thumbnail, update_modified=False)
 			except Exception:
-				print "Unable to make thumbnail for {0}".format(item.website_image.encode("utf-8"))
+				print("Unable to make thumbnail for {0}".format(item.website_image.encode("utf-8")))
diff --git a/erpnext/patches/v6_4/repost_gle_for_journal_entries_where_reference_name_missing.py b/erpnext/patches/v6_4/repost_gle_for_journal_entries_where_reference_name_missing.py
index e0268c4..1319b53 100644
--- a/erpnext/patches/v6_4/repost_gle_for_journal_entries_where_reference_name_missing.py
+++ b/erpnext/patches/v6_4/repost_gle_for_journal_entries_where_reference_name_missing.py
@@ -1,7 +1,7 @@
 # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
 # License: GNU General Public License v3. See license.txt
 
-from __future__ import unicode_literals
+from __future__ import print_function, unicode_literals
 import frappe
 
 def execute():
@@ -13,7 +13,7 @@
 				and against_voucher=je.reference_name)""")
 
 	for d in je_list:
-		print d
+		print(d)
 		
 		# delete existing gle
 		frappe.db.sql("delete from `tabGL Entry` where voucher_type='Journal Entry' and voucher_no=%s", d)
diff --git a/erpnext/patches/v6_6/fix_website_image.py b/erpnext/patches/v6_6/fix_website_image.py
index b3b4cab..cc3e2d8 100644
--- a/erpnext/patches/v6_6/fix_website_image.py
+++ b/erpnext/patches/v6_6/fix_website_image.py
@@ -1,4 +1,4 @@
-from __future__ import unicode_literals
+from __future__ import print_function, unicode_literals
 import frappe
 from frappe.utils import encode
 
@@ -25,7 +25,7 @@
 		try:
 			file.validate_file()
 		except IOError:
-			print encode(item.website_image), "does not exist"
+			print(encode(item.website_image), "does not exist")
 			file.delete()
 			item.db_set("website_image", None, update_modified=False)
 
diff --git a/erpnext/patches/v7_0/fix_nonwarehouse_ledger_gl_entries_for_transactions.py b/erpnext/patches/v7_0/fix_nonwarehouse_ledger_gl_entries_for_transactions.py
index 58da059..2bc0971 100644
--- a/erpnext/patches/v7_0/fix_nonwarehouse_ledger_gl_entries_for_transactions.py
+++ b/erpnext/patches/v7_0/fix_nonwarehouse_ledger_gl_entries_for_transactions.py
@@ -1,7 +1,7 @@
 # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
 # License: GNU General Public License v3. See license.txt
 
-from __future__ import unicode_literals
+from __future__ import print_function, unicode_literals
 import frappe, erpnext
 
 def execute():
@@ -35,11 +35,11 @@
 				voucher.make_gl_entries()
 				frappe.db.commit()
 			except Exception as e:
-				print frappe.get_traceback()
+				print(frappe.get_traceback())
 				rejected.append([voucher_type, voucher_no])
 				frappe.db.rollback()
 
-		print rejected
+		print(rejected)
 
 def set_warehouse_for_stock_account(warehouse_account):
 	for account in warehouse_account:
diff --git a/erpnext/patches/v7_0/rename_salary_components.py b/erpnext/patches/v7_0/rename_salary_components.py
index 4e9ceb2..8409ca8 100644
--- a/erpnext/patches/v7_0/rename_salary_components.py
+++ b/erpnext/patches/v7_0/rename_salary_components.py
@@ -81,7 +81,7 @@
 		try:
 			frappe.db.sql("""INSERT INTO `tabSalary Component` ({0}) SELECT {1} FROM `tab{2}`"""
 				.format(target_cols, source_cols, doctype))
-		except Exception, e:
+		except Exception as e:
 			if e.args[0]==1062:
 				pass
 			
diff --git a/erpnext/schools/api.py b/erpnext/schools/api.py
index c613c8c..ff2da07 100644
--- a/erpnext/schools/api.py
+++ b/erpnext/schools/api.py
@@ -63,7 +63,7 @@
 	:param student_group: Student Group.
 	:param date: Date.
 	"""
-	 
+
 	present = json.loads(students_present)
 	absent = json.loads(students_absent)
 	
diff --git a/erpnext/schools/doctype/student_applicant/student_applicant.py b/erpnext/schools/doctype/student_applicant/student_applicant.py
index 047c702..081fa06 100644
--- a/erpnext/schools/doctype/student_applicant/student_applicant.py
+++ b/erpnext/schools/doctype/student_applicant/student_applicant.py
@@ -2,7 +2,7 @@
 # Copyright (c) 2015, Frappe Technologies and contributors
 # For license information, please see license.txt
 
-from __future__ import unicode_literals
+from __future__ import print_function, unicode_literals
 import frappe
 from frappe import _
 from frappe.model.document import Document
@@ -13,7 +13,7 @@
 		if self.student_admission:
 			naming_series = frappe.db.get_value('Student Admission', self.student_admission,
 				'naming_series_for_student_applicant')
-			print naming_series
+			print(naming_series)
 
 			if naming_series:
 				self.naming_series = naming_series
diff --git a/erpnext/schools/doctype/student_group/test_student_group.py b/erpnext/schools/doctype/student_group/test_student_group.py
index 18a6b14..e358c27 100644
--- a/erpnext/schools/doctype/student_group/test_student_group.py
+++ b/erpnext/schools/doctype/student_group/test_student_group.py
@@ -8,20 +8,20 @@
 from frappe.utils.make_random import get_random
 
 class TestStudentGroup(unittest.TestCase):
-	 def test_student_roll_no(self):
-	 	doc = frappe.get_doc({
-	 		"doctype": "Student Group",
-	 		"student_group_name": "_Test Student Group R",
+	def test_student_roll_no(self):
+		doc = frappe.get_doc({
+			"doctype": "Student Group",
+			"student_group_name": "_Test Student Group R",
 			"group_based_on": "Activity"
-	 		}).insert()
+			}).insert()
 
-	 	student_list = []
-	 	while len(student_list) < 3:
-	 		s = get_random("Student")
-	 		if s not in student_list:
-	 			student_list.append(s)
+		student_list = []
+		while len(student_list) < 3:
+			s = get_random("Student")
+			if s not in student_list:
+				student_list.append(s)
 
-	 	doc.extend("students", [{"student":d} for d in student_list])
-	 	doc.save()
-	 	self.assertEquals(max([d.group_roll_number for d in doc.students]), 3)
+		doc.extend("students", [{"student":d} for d in student_list])
+		doc.save()
+		self.assertEquals(max([d.group_roll_number for d in doc.students]), 3)
 
diff --git a/erpnext/selling/doctype/customer/customer.py b/erpnext/selling/doctype/customer/customer.py
index d797632..c12cd44 100644
--- a/erpnext/selling/doctype/customer/customer.py
+++ b/erpnext/selling/doctype/customer/customer.py
@@ -67,7 +67,7 @@
 		'''If Customer created from Lead, update lead status to "Converted"
 		update Customer link in Quotation, Opportunity'''
 		if self.lead_name:
-		 	frappe.db.set_value('Lead', self.lead_name, 'status', 'Converted', update_modified=False)
+			frappe.db.set_value('Lead', self.lead_name, 'status', 'Converted', update_modified=False)
 
 			for doctype in ('Opportunity', 'Quotation'):
 				for d in frappe.get_all(doctype, {'lead': self.lead_name}):
diff --git a/erpnext/selling/doctype/sales_order/sales_order.py b/erpnext/selling/doctype/sales_order/sales_order.py
index 396b1c2..5f904c2 100644
--- a/erpnext/selling/doctype/sales_order/sales_order.py
+++ b/erpnext/selling/doctype/sales_order/sales_order.py
@@ -51,9 +51,9 @@
 		# validate p.o date v/s delivery date
 		if self.po_date:
 			for d in self.get("items"):
-				 if d.delivery_date and getdate(self.po_date) > getdate(d.delivery_date):
-					 frappe.throw(_("Row #{0}: Expected Delivery Date cannot be before Purchase Order Date")
-					 	.format(d.idx))
+				if d.delivery_date and getdate(self.po_date) > getdate(d.delivery_date):
+					frappe.throw(_("Row #{0}: Expected Delivery Date cannot be before Purchase Order Date")
+						.format(d.idx))
 
 		if self.po_no and self.customer:
 			so = frappe.db.sql("select name from `tabSales Order` \
diff --git a/erpnext/setup/doctype/item_group/test_item_group.py b/erpnext/setup/doctype/item_group/test_item_group.py
index bc88132..c487c72 100644
--- a/erpnext/setup/doctype/item_group/test_item_group.py
+++ b/erpnext/setup/doctype/item_group/test_item_group.py
@@ -1,7 +1,7 @@
 # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
 # License: GNU General Public License v3. See license.txt
 
-from __future__ import unicode_literals
+from __future__ import print_function, unicode_literals
 import unittest
 import frappe
 from frappe.utils.nestedset import NestedSetRecursionError, NestedSetMultipleRootsError, \
@@ -112,7 +112,7 @@
 
 	def print_tree(self):
 		import json
-		print json.dumps(frappe.db.sql("select name, lft, rgt from `tabItem Group` order by lft"), indent=1)
+		print(json.dumps(frappe.db.sql("select name, lft, rgt from `tabItem Group` order by lft"), indent=1))
 
 	def test_move_leaf_into_another_group(self):
 		# before move
diff --git a/erpnext/setup/install.py b/erpnext/setup/install.py
index 7b71675..9bf15ce 100644
--- a/erpnext/setup/install.py
+++ b/erpnext/setup/install.py
@@ -1,7 +1,7 @@
 # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
 # License: GNU General Public License v3. See license.txt
 
-from __future__ import unicode_literals
+from __future__ import print_function, unicode_literals
 
 import frappe
 from frappe import _
@@ -19,10 +19,10 @@
 
 def check_setup_wizard_not_completed():
 	if frappe.db.get_default('desktop:home_page') == 'desktop':
-		print
-		print "ERPNext can only be installed on a fresh site where the setup wizard is not completed"
-		print "You can reinstall this site (after saving your data) using: bench --site [sitename] reinstall"
-		print
+		print()
+		print("ERPNext can only be installed on a fresh site where the setup wizard is not completed")
+		print("You can reinstall this site (after saving your data) using: bench --site [sitename] reinstall")
+		print()
 		return False
 
 def set_single_defaults():
diff --git a/erpnext/stock/doctype/delivery_note/delivery_note.py b/erpnext/stock/doctype/delivery_note/delivery_note.py
index 82beff8..f5a99af 100644
--- a/erpnext/stock/doctype/delivery_note/delivery_note.py
+++ b/erpnext/stock/doctype/delivery_note/delivery_note.py
@@ -92,9 +92,9 @@
 	def so_required(self):
 		"""check in manage account if sales order required or not"""
 		if frappe.db.get_value("Selling Settings", None, 'so_required') == 'Yes':
-			 for d in self.get('items'):
-				 if not d.against_sales_order:
-					 frappe.throw(_("Sales Order required for Item {0}").format(d.item_code))
+			for d in self.get('items'):
+				if not d.against_sales_order:
+					frappe.throw(_("Sales Order required for Item {0}").format(d.item_code))
 
 	def validate(self):
 		self.validate_posting_time()
diff --git a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
index d12c288..2d089c4 100644
--- a/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
+++ b/erpnext/stock/doctype/purchase_receipt/purchase_receipt.py
@@ -84,9 +84,9 @@
 
 	def po_required(self):
 		if frappe.db.get_value("Buying Settings", None, "po_required") == 'Yes':
-			 for d in self.get('items'):
-				 if not d.purchase_order:
-					 frappe.throw(_("Purchase Order number required for Item {0}").format(d.item_code))
+			for d in self.get('items'):
+				if not d.purchase_order:
+					frappe.throw(_("Purchase Order number required for Item {0}").format(d.item_code))
 
 	def get_already_received_qty(self, po, po_detail):
 		qty = frappe.db.sql("""select sum(qty) from `tabPurchase Receipt Item`
diff --git a/erpnext/stock/doctype/stock_settings/stock_settings.py b/erpnext/stock/doctype/stock_settings/stock_settings.py
index d9d9568..186eaee 100644
--- a/erpnext/stock/doctype/stock_settings/stock_settings.py
+++ b/erpnext/stock/doctype/stock_settings/stock_settings.py
@@ -18,7 +18,7 @@
 			self.get("item_naming_by")=="Naming Series", hide_name_field=True)
 
 		stock_frozen_limit = 356
-		submitted_stock_frozen = self.stock_frozen_upto_days
+		submitted_stock_frozen = self.stock_frozen_upto_days or 0
 		if submitted_stock_frozen > stock_frozen_limit:
 			self.stock_frozen_upto_days = stock_frozen_limit
 			frappe.msgprint (_("`Freeze Stocks Older Than` should be smaller than %d days.") %stock_frozen_limit)
diff --git a/erpnext/stock/stock_balance.py b/erpnext/stock/stock_balance.py
index 403d5cb..6a4ac43 100644
--- a/erpnext/stock/stock_balance.py
+++ b/erpnext/stock/stock_balance.py
@@ -1,7 +1,7 @@
 # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
 # License: GNU General Public License v3. See license.txt
 
-from __future__ import unicode_literals
+from __future__ import print_function, unicode_literals
 import frappe
 
 from frappe.utils import flt, cstr, nowdate, nowtime
@@ -170,7 +170,7 @@
 			where item_code=%s and warehouse=%s and docstatus < 2""", (d[0], d[1]))
 
 		if serial_nos and flt(serial_nos[0][0]) != flt(d[2]):
-			print d[0], d[1], d[2], serial_nos[0][0]
+			print(d[0], d[1], d[2], serial_nos[0][0])
 
 		sle = frappe.db.sql("""select valuation_rate, company from `tabStock Ledger Entry`
 			where item_code = %s and warehouse = %s and ifnull(is_cancelled, 'No') = 'No'
@@ -244,7 +244,7 @@
 	i = 0
 	for voucher_type, voucher_no in vouchers:
 		i+=1
-		print i, "/", len(vouchers), voucher_type, voucher_no
+		print(i, "/", len(vouchers), voucher_type, voucher_no)
 		try:
 			for dt in ["Stock Ledger Entry", "GL Entry"]:
 				frappe.db.sql("""delete from `tab%s` where voucher_type=%s and voucher_no=%s"""%
@@ -259,9 +259,9 @@
 			doc.update_stock_ledger()
 			doc.make_gl_entries(repost_future_gle=False)
 			frappe.db.commit()
-		except Exception, e:
-			print frappe.get_traceback()
+		except Exception as e:
+			print(frappe.get_traceback())
 			rejected.append([voucher_type, voucher_no])
 			frappe.db.rollback()
 
-	print rejected
+	print(rejected)
diff --git a/erpnext/templates/utils.py b/erpnext/templates/utils.py
index e46fed6..6ebe411 100644
--- a/erpnext/templates/utils.py
+++ b/erpnext/templates/utils.py
@@ -36,11 +36,11 @@
     ))
 
     if customer:
-    	opportunity.customer = customer[0][0]
+        opportunity.customer = customer[0][0]
     elif lead:
-    	opportunity.lead = lead
+        opportunity.lead = lead
     else:
-    	opportunity.lead = new_lead.name
+        opportunity.lead = new_lead.name
 
     opportunity.insert(ignore_permissions=True)
 
diff --git a/erpnext/utilities/__init__.py b/erpnext/utilities/__init__.py
index 944f978..0f641b2 100644
--- a/erpnext/utilities/__init__.py
+++ b/erpnext/utilities/__init__.py
@@ -1,5 +1,5 @@
 ## temp utility
-
+from __future__ import print_function
 import frappe
 from erpnext.utilities.activation import get_level
 from frappe.utils import cstr
@@ -12,7 +12,7 @@
 
 		for f in dt.fields:
 			if f.fieldname == d.fieldname and f.fieldtype in ("Text", "Small Text"):
-				print f.parent, f.fieldname
+				print(f.parent, f.fieldname)
 				f.fieldtype = "Text Editor"
 				dt.save()
 				break