0

I want to import the functions defined in a file like described in this question into a test (don't ask about why... combination of legacy system and plugin system)

import imp

module = imp.load_source('module', '/path/to/file.py')
module.some_function()

but got NameError: name '<SOME-VARIABLE>' is not defined.

'<SOME-VARIABLE>' is not defined in /path/to/file.py, but in /another/path/to/another/file.py. Is it possible to monkey patch <SOME-VARIABLE> it to allow import of the file/module?

thinwybk
  • 4,193
  • 2
  • 40
  • 76
  • According to the error message, it is _not_ defined in `/path/to/file.py`, or at least not before it is used. Can you show the contents of `/path/to/file.py`? And can you change the contents of that file, or is it a fixed requirement? – lxop Nov 30 '18 at 14:06
  • Typo in the question. Adjusted accordingly. Sry. I cannot show the content. However it's only important to know that `` is defined in `/another/path/to/another/file.py`. – thinwybk Dec 01 '18 at 12:23
  • Are you able to edit `/path/to/file.py`? Because it sounds like that file is broken if it is trying to use a variable that it hasn't created or imported – lxop Dec 01 '18 at 14:05
  • No I cannot modify `/path/to/file.py`. This behaviour relates to the plugin system involved. During application runtime `` is created and may be used in the production code `/path/to/file.py`. I'm only interested in if it's possible to monkey patch during import in general. – thinwybk Dec 01 '18 at 14:27
  • If it's failing during import, then that means that `` is being used at the top level of the file, and if that variable hasn't been created or imported yet then the code is broken. It won't be possible to import that file from anywhere because the file cannot be executed successfully – lxop Dec 01 '18 at 14:40

0 Answers0