2

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,

1
  • 1
    While I'm not entirely sure on why this works openpyxl but not time, I would just like to add that it is not uncommon to have your custom classes as their own modules such that in your main script you would then call 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 Commented Jan 26, 2017 at 14:05

2 Answers 2

3

Having imports within the class definition makes those names available as class members:

>>> class Example:
        import time

>>> Example.time
<module 'time' (built-in)>

So in order to access those imports from within, you would have to refer to them as class members using MyCustomClass.xl.load_workbook.

In general though, this is discouraged. Those imports do not belong to the class itself, they are a dependency of the class but there is no reason to expose those dependencies as class members. Instead, you should just import those names outside of the class definition:

import openpyxl as xl
from fuzzywuzzy import fuzz
from fuzzywuzzy import process
import time

class MyCustomClass:
    def __init__(self, file_path):
        self.file = xl.load_workbook(file_path)

    def some_method(self):
        start = time.time()
        process.extractOne(some_args)
Sign up to request clarification or add additional context in comments.

1 Comment

thanks a lot, @poke. I did not realize this could be down from outside, though it make sense.
0

Why not import them at the top of the fie before outside of your class definition? Or if you really want these imports to be very localized import them inside of the __init__ method or method that is actually using them.

The way you are doing it your imported module references are neither in the global scope nor in the method local scope, that's why it doesn't work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.