2

So I'm working on implementing a websocket client protocol on a robot called NAO. Without going into too many details, I can't do a normal import because a variable is needed before I can do any import statements, so I need to handle my import statements in a method.

Problem is is that I need these packages across multiple classes in a single file, and therefore I REALLY need to make these imports global.

Now I found another StackOverflow post handling pretty much the same thing:

Python: how to make global imports from a function

However, this post only handles regular imports, not class imports (there is probably a correct name for this, but I don't know it)

Does anybody have an idea on how to handle this?

Community
  • 1
  • 1

1 Answers1

3

You mean something like this?

>>> def f():
...     global pi
...     from math import pi
...
>>> pi
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'pi' is not defined
>>> f()
>>> pi
3.141592653589793
>>>
Dunno
  • 3,632
  • 3
  • 28
  • 43
  • Well, I tried this before, but I wasn't sure that this was the correct approach, so I abandoned it when I received a different error. But your answer has given me some insight into this new error. It seems that importing from a method causes classes that have an imported superclass to throw an error. This behaviour can be replicated on desktop as well. I realise this is a totally different question, but I might as well ask: Do you know any way around this? – user2983738 Apr 11 '17 at 14:54
  • @user2983738 hm maybe I could help but I'd need some more details. Maybe just make a new question on SO? It's hard to debug code without seeing it :) – Dunno Apr 11 '17 at 15:14