2

if I supply ("pandas", "pd"), is it possible to obtain import pandas as pd programatically?

I do know that I can do:

import importlib
pd = importlib.import_module('pandas')

But, can I supply the pd somehow as a string as well?

0

2 Answers 2

4

Technically, yes, and in more than way:

locals()['pd'] = importlib.import_module('pandas')

Of course, it rather bad practice.

Sign up to request clarification or add additional context in comments.

2 Comments

OK. I am actually trying to get ipython to import some modules upon startup if they exist.
Why don't you just use try…except to handle the case of missing module?
0

Yes.

name = 'pd'
exec(f'import pandas as {name}')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.