1

When I run python a/b.py, I get:

Traceback (most recent call last):
  File "a/b.py", line 10, in <module>
    from . import config
ImportError: cannot import name 'config'

When I run python -m a.b, I get no error.

What is the fundamental difference between the two approaches?

mawimawi
  • 4,222
  • 3
  • 33
  • 52
Jinhua Fu
  • 29
  • 2
  • Judging from your error this is an issue of relative imports. Check out the information in [this question](https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time) And [this question](https://stackoverflow.com/questions/16981921/relative-imports-in-python-3) has a little more info on being able to run a file both ways you have listed – PyPingu Jun 17 '19 at 15:27
  • 1
    IMHO, as a general rule, if `a` is a package **never** run anything inside it. It's usually better if whoever makes/distributes the package to provide some external script that does stuff (e.g. using `setuptools`'s `console_scripts`). – Bakuriu Jun 17 '19 at 20:31

0 Answers0