classification
Title: import and __import__() fails silently without a ImportError and does not add the module to the file's namespace.
Type: Stage: resolved
Components: Versions: Python 3.5
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: Decorater, abarry, r.david.murray
Priority: normal Keywords:

Created on 2016-07-28 11:58 by Decorater, last changed 2016-07-28 12:24 by r.david.murray. This issue is now closed.

Messages (6)
msg271533 - (view) Author: Decorater (Decorater) * Date: 2016-07-28 11:58
So, I have some code. I tried to make a 'plugin' for my bot I made in python. However it seems to not be able to import it which it should be able to.

The code I used is here: https://bpaste.net/show/e4445c47490d

I dont even know why it is not addign it to the file's namespace or even adding it to sys.modules either.
msg271534 - (view) Author: Anilyka Barry (abarry) * (Python triager) Date: 2016-07-28 12:07
`import` merely adds the imported module to the current namespace which, in your code, is some local (non-global) namespace. It is successfully imported but never used, and quickly falls out of scope.

You also check for `testplugin in sys.modules`, but you want `'testplugin' in sys.modules`. Closing this as not a bug, please reopen if there's indeed a bug I didn't spot.
msg271536 - (view) Author: Decorater (Decorater) * Date: 2016-07-28 12:16
yeah I just noticed it is in sys.modules but does not get defined globally. Bug maybe there is a hack to make it global?
msg271537 - (view) Author: Decorater (Decorater) * Date: 2016-07-28 12:16
But*
msg271539 - (view) Author: Anilyka Barry (abarry) * (Python triager) Date: 2016-07-28 12:21
Yeah, just add 'global testplugin' at the top of your function, before your import it (you'll also need a global statement if you want to delete/re-import it). You might want to take a look at importlib if you wish to dynamically load modules (especially reloading them, which is a pain).
msg271540 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-07-28 12:24
Questions like this are more appropriate for the python-list mailing list, or even python-tutor.
History
Date User Action Args
2016-07-28 12:24:50r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg271540

resolution: not a bug
2016-07-28 12:21:04abarrysetmessages: + msg271539
2016-07-28 12:16:31Decoratersetmessages: + msg271537
2016-07-28 12:16:01Decoratersetstatus: closed -> open
resolution: not a bug -> (no value)
messages: + msg271536
2016-07-28 12:07:24abarrysetstatus: open -> closed

nosy: + abarry
messages: + msg271534

resolution: not a bug
stage: resolved
2016-07-28 11:58:25Decoratercreate