简体   繁体   中英

Python. Adding custom methods to existing site-packages

I'm using openpyxl with python 2.7 to manipulate some workbooks. I've implemented my own methods for class Worksheet except they're not being found when I try to access them. I''m on windows and I've adding the following code to site-packages/openpyxl/worksheet/worksheet.py. Source code here .

class Worksheet(_WorkbookChild):
"""
Library defined methods here.
"""

    #My Method.
    def hello_world(self):
       print "Hello from worksheet."

When I open up a workbook and try and call my function it raises an error for 'no attribute' even though it's been defined (properly?) under the scope of the class Worksheet.

import openpyxl

wb = openpyxl.load_workbook('helloworld.xlsx')
sheet = wb.get_active_sheet()

sheet.hello_world()
AttributeError: 'Worksheet' object has no attribute 'hello_world'

Do I need to do anything differently to update the existing library? I've deleted the .pyc, recompiled and am still getting the same error.

For anyone else who runs into this problem. The library was using 4 spaces for each indent while I was using a regular tab. The variation in indents caused the method to not be defined even though it appeared to be.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM