0

I'm newbie on python so please understand me if this question is too easy. I already searched out but I didn't understand accurately their answers.


My question : What point I missed out for understanding python import system?

My project structure is like this (https://github.com/dingyo777/python_import_test):

-- python_import_test
    |-- deco
    |    |-- printer.py
    |    |-- decorator.py
    |
    |-- test.py
  • printer.py imports decorater.py
  • test.py imports printer.py

When I run python test.py, error is like this

Traceback (most recent call last):
  File "test.py", line 5, in <module>
    import deco.printer
  File "/Users/dingyo/Projects/my_repo/python_import_test/deco/printer.py", line 3, in <module>
    import decorator
ModuleNotFoundError: No module named 'decorator'

Of course, it will be helpful if someone tell me how to resolve this error. But more thing what I want to know is understanding how python import package or module accurately.

Would you mind to explain what point I missed out?

obanadingyo
  • 317
  • 1
  • 2
  • 14
  • 1
    Possible duplicate of [How to import other Python files?](https://stackoverflow.com/questions/2349991/how-to-import-other-python-files) – MyNameIsCaleb Sep 25 '19 at 01:45
  • Also look at [this question](https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time) and generally search `python import` as there are a lot of really good questions/answers on the topic. – MyNameIsCaleb Sep 25 '19 at 01:47
  • @MyNameIsCaleb Thank you for your advice :) Let me check. – obanadingyo Sep 25 '19 at 04:42

1 Answers1

0
cd deco/
vim __init__.py

┓( ´-` )┏

oaixnah
  • 44
  • 2
  • 4
    Welcome to Stack Overflow. This answer has no explanation and as such is likely to attract down votes. Please don't include emoticon's in answers. – Jason Aller Sep 25 '19 at 03:44
  • 1
    @JasonAller Thank you for your response. Please refer to https://github.com/dingyo777/python_import_test It has `__init__.py` already. ┓( ´-` )┏ – obanadingyo Sep 25 '19 at 04:39
  • printer.py plan1. line3 `import decorator` changed to `import deco.decorator as decorator` or plan2. line3 `import decorator` changed to `import deco.decorator` and line6 `print(decorator.decorate("test"))` changed to `print(deco.decorator.decorate("test"))` – oaixnah Sep 25 '19 at 05:52
  • ``` import sys print(sys.path) from .decorator import decorate def dec_print(): print(decorate("test")) dec_print() ``` – oaixnah Sep 25 '19 at 05:59
  • You need to specify where the imported module or function comes from – oaixnah Sep 25 '19 at 06:00
  • @oaixnah Thank you for your advice :) It was really helpful for me. – obanadingyo Sep 25 '19 at 23:27
  • @oaixnah May I ask one thing? What is difference between `import deco.decorator` and `import decorator`? Why it works well when change to `import deco.decorator`? – obanadingyo Sep 25 '19 at 23:37