1

I'm having a hard time understanding how imports work in Python 3.x. Suppose I have a project structure like this

test
../foo
....foo1.py
....foo2.py
../bar
....bar1.py

My understanding is that foo and bar can be treated as packages and the .py files as modules.

I want to import foo1 and bar1 in foo2(which is the main script). How can that be done in both absolute and relative ways?

If I want to execute foo2 after this, is this a bad project structure (maybe foo2 should be placed elsewhere, but I do not want to place it at the root)?

Alison
  • 11
  • 1
  • The answers to the question [Relative imports for the billionth time](https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time) might be helpful reading. – martineau Dec 13 '18 at 00:45

1 Answers1

0

Try this:

import importlib

name = input('Enter module name:')
importlib.import_module(name)

If you want more information on importlib, check https://docs.python.org/3/library/importlib.html#module-importlib