Skip to content Skip to sidebar Skip to footer

Python Import Works When Running Module But Breaks When This Module Is Imported

I have a directory structure like this: source\ main\ bar.py run.py A\ foo.py bar.py has functions which foo.py needs, so I use from bar import *, which works, as

Solution 1:

You should avoid fiddling with the import paths as long as you have other options. In this case, create empty files main\__init__.py and A\__init__.py so Python recognizes these directories as packages, replace bar with main.bar in foo.py and run it from the top source directory.

Now, importing functions from foo.py to run.py should be as easy as:

fromA.foo import fooFun1, fooFun2

Post a Comment for "Python Import Works When Running Module But Breaks When This Module Is Imported"