cqcn1991
V2EX  ›  问与答

求教一个 Python import 的问题

  •  
  •   cqcn1991 · Jan 30, 2016 · 1577 views
    This topic created in 3781 days ago, the information mentioned may be changed or developed.

    我有这样一些 Module:

    enter image description here

    因为都涉及到 scipy, 所以每个文件开头,经常会要这样 import

    from matplotlib import pyplot as plt
    import numpy as np
    import matplotlib as mpl
    

    我想请教一下,有没有办法,可以把这个相同的部分抽出来,这样子,我可以减少一些重复的代码?

    同步贴: http://stackoverflow.com/questions/35102484/python-how-to-share-import-between-odules

    2 replies    2016-01-31 04:22:13 +08:00
    Kirscheis
        1
    Kirscheis  
       Jan 30, 2016 via Android
    写到一个文件里然后手动写 __all__ 然后在其他文件里 from import * 不就得了,但是这么省一行代码有啥意义。。不方便读代码,也不能节省开销
    ljcarsenal
        2
    ljcarsenal  
       Jan 31, 2016 via Android
    在那个__init. py 里写
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1073 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 23:16 · PVG 07:16 · LAX 16:16 · JFK 19:16
    ♥ Do have faith in what you're doing.