Python import modules in another file

前端 未结 2 1727
眼角桃花
眼角桃花 2021-02-09 13:59

I\'m currently re-factoring a project (formerly big one file) into several seperate python files, each of which runs a specific part of my application. Eg, GUIthread.py

2条回答
  •  北恋
    北恋 (楼主)
    2021-02-09 14:47

    The other answer shows how what you want is (sort of) possible, but didn't address your second question about good practice.

    Using import * is almost invariably considered bad practice. See "Why is import * bad?" and "Importing * from a package" from the docs.

    Remember from PEP 20 that explicit is better than implicit. With explicit, specific imports (e.g. from math import sqrt) in every module, there is never confusion about from where a name came, your module's namespace includes only what it needs, and bugs are prevented.

    The downside of having to write a couple import statements per module does not outweigh the potential problems introduced by trying to get around writing them.

提交回复
热议问题