I'm new to Python and I'm struggling with importing external modules / packages inside a custom class. I did not found answers (probably because the terms I used are not the good ones) so I post here. It's basically a newbie question dealing with Python global understanding
I've the following class:
class MyCustomClass:
import openpyxl as xl
from fuzzywuzzy import fuzz
from fuzzywuzzy import process
import time
def __init__(self, file_path):
self.file = xl.load_workbook(file_path)
def some_method(self):
start = time.time()
process.extractOne(#calling args)
When I create an instance with this class structure I got `NameError: name 'xl' is not defined``
It worked when using self.xl and self.time.
Here are my questions:
1) What is the good way to import module or packages in my class?
2) Can I avoid to make them instance variables?
Thanks for your help,
from (filename) import MyCustomClass. This limits the scope of your imports so you don't have to worry about namespaces without requiting them to be in the constructor of your objects