Environment:
- Python: 3.9
- Virtualenv under PyCharm
In the current source code I'm writing I try to:
- Dynamically check if the required dependencies are installed
- If not, a message appears and the program stops, otherwise, go to 3
- The source code dynamically imports the installed module
- The code uses the module as usual when it is required
The thing is that the code is failing at (4) with the tkfilebrowser module. This is my code for importing:
already_imported = {}
def do_import(dependency):
"""
Imports the dependency module
:param dependency: the name of the dependency module to import
:raise: ModuleNotFoundError if not found
:return: the imported module
"""
global already_imported
if dependency in already_imported:
result_module = already_imported[dependency]
else:
result_module = importlib.import_module(dependency)
already_imported[dependency] = result_module
return result_module
Just after the call, this is the context:
>>> tkfilebrowser = do_import('tkfilebrowser')
>>> dir(tkfilebrowser)
['__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']
>>> type(tkfilebrowser)
<class 'module'>
>>> print(tkfilebrowser)
<module 'tkfilebrowser' (namespace)>
>>> tkfilebrowser.askopendirname()
AttributeError: module 'tkfilebrowser' has no attribute 'askopendirname'
I couldn't guess what's wrong here and what I should be doing for using the module correctly.
CodePudding user response:
Everything was working in the source code.
The problem was that I was using an incorrect Python binary in the Run configuration of PyCharm so, as soon as I realized, I changed it and all come along well.
So if anyone comes here with a similar error, just make really sure that your virtualenv or whatever is the Python environment you have configured is really being used and not another one.
In PyCharm:
Edit configurations => Python interpreter