What is the most pythonic way to import modules in python

前端 未结 4 2039
囚心锁ツ
囚心锁ツ 2020-12-03 15:26

Can anyone suggest me what is the most pythonic way to import modules in python? Let me explain - i have read a lot of python code and found several different ways of how t

4条回答
  •  一个人的身影
    2020-12-03 15:32

    Do not use from module import *. This will pollute the namespace and is highly frowned upon. However, you can import specific things using from; from module import something. This keeps the namespace clean. On larger projects if you use a wildcard you could be importing 2 foo or 2 bar into the same namespace.

    PEP 8 says to have imports on separate lines. For instance:

    import os
    import sys
    import yourmodule
    from yourmodule import specific_stuff
    

    One thing I do is alphabetize my imports into two groups. One is std/third party and the second is internal modules.

提交回复
热议问题