Y
Y
YariKartoshe4ka2019-08-11 21:58:57
Python
YariKartoshe4ka, 2019-08-11 21:58:57

How to import a module from a variable?

I wrote a code that checks for the presence of a module:
try:
import this
except ModuleNotFoundError:
system('pip install this') The
question is, how can I programmatically substitute other modules instead of 'this'?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
sim3x, 2019-08-11
@YariKartoshe4ka

You don’t need to want this
. Better the application will swear that there are no packages, and the user will manually create the environment and install the dependencies.
You only need to create requirements.txt / Pipfile and indicate in the manual that manual installation of dependencies is required
Or create a package and write the same for "auto " install dependencies from the same files in setup.py

S
Sergey Gornostaev, 2019-08-12
@sergey-gornostaev

You can use importlib . But don't, the idea of ​​automatically installing modules is a bad idea.

B
Bibletoon, 2019-08-11
@bibletoon

Use eval

L
lightmanLP, 2019-08-12
@lightmanLP

exec or eval. exec('try:\n import '+this+'\nexcept...')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question