【问题标题】:Python: calling another python scriptPython:调用另一个 Python 脚本
【发布时间】:2013-06-01 21:15:11
【问题描述】:

是否可以调用另一个 python 脚本来仅访问脚本中的定义而不访问其他内容?

在我要导入的脚本中,有一些我想抑制的情节,因为这个其他程序不需要。也就是说,我只想访问 Stumpff 函数的定义而不绘制图形。

我要导入的脚本是:

#!/usr/bin/env ipython
#  This program plots the Stumpff functions C(z) and S(z)

import numpy as np
import pylab
from matplotlib.ticker import MaxNLocator


def C(z):
    if z > 0:
        return (1 - np.cos(z ** 0.5)) / z
    elif z < 0:
        return (np.cosh(np.sqrt(-z)) - 1) / -z
    return 0.5


def S(z):
    if z > 0:
        return (np.sqrt(z) - np.sin(z ** 0.5)) / np.sqrt(z) ** 3
    elif z < 0:
        return (np.sinh(np.sqrt(-z)) - np.sqrt(-z)) / np.sqrt(-z) ** 3
    return 1.0 / 6.0


vC = np.vectorize(C)
vS = np.vectorize(S)

z = np.linspace(-50.0, 500.0, 100000.0)
y = vC(z)
y2 = vS(z)

fig = pylab.figure()
ax = fig.add_subplot(111)
ax.plot(z, y, 'r')
ax.plot(z, y2, 'b')
pylab.legend(('$C(z)$', '$S(z)$'), loc = 0)
pylab.xlim((-50, 0))
pylab.ylim((0, 12))
pylab.xlabel('$z$')
pylab.gca().xaxis.set_major_locator(MaxNLocator(prune = 'lower'))
pylab.savefig('stumpffneg50to0.eps', format = 'eps')


fig2 = pylab.figure()
ax2 = fig2.add_subplot(111)
ax2.plot(z, y, 'r')
ax2.plot(z, y2, 'b')
pylab.legend(('$C(z)$', '$S(z)$'), loc = 1)
pylab.xlim((0, 30))
pylab.ylim((0, 0.5))
pylab.xlabel('$z$')
pylab.gca().xaxis.set_major_locator(MaxNLocator(prune = 'lower'))
pylab.savefig('stumpff0to30.eps', format = 'eps')


fig3 = pylab.figure()
ax3 = fig3.add_subplot(111)
ax3.plot(z, y, 'r')
ax3.plot(z, y2, 'b')
pylab.legend(('$C(z)$', '$S(z)$'), loc = 0)
pylab.xlim((0, 500))
pylab.ylim((0, 0.05))
pylab.xlabel('$z$')
pylab.gca().xaxis.set_major_locator(MaxNLocator(prune = 'lower'))
pylab.savefig('stumpff0to500.eps', format = 'eps')
pylab.show()

通过阅读python how do I call external python programs,我看到我已经添加了

import stumpff

之后,我的新脚本会理解C(z)S(z)吗?

【问题讨论】:

    标签: python import subprocess


    【解决方案1】:

    你的脚本是这样写的,没有办法导入它,也没有制作情节。

    为了使import stumpff 能够工作,并且您的脚本能够理解 C(z) 和 S(z),您需要制作绘图代码,使其仅在您作为脚本。一种方法是把它全部放在一个 main() 函数中,然后使用

    if __name__ == '__main__':
        main()
    

    或者,只需将其全部置于该条件下,如下所示:

    #!/usr/bin/env ipython
    #  This program plots the Stumpff functions C(z) and S(z)
    
    import numpy as np
    import pylab
    from matplotlib.ticker import MaxNLocator
    
    
    def C(z):
        if z > 0:
            return (1 - np.cos(z ** 0.5)) / z
        elif z < 0:
            return (np.cosh(np.sqrt(-z)) - 1) / -z
        return 0.5
    
    
    def S(z):
        if z > 0:
            return (np.sqrt(z) - np.sin(z ** 0.5)) / np.sqrt(z) ** 3
        elif z < 0:
            return (np.sinh(np.sqrt(-z)) - np.sqrt(-z)) / np.sqrt(-z) ** 3
        return 1.0 / 6.0
    
    
    if __name__ == '__main__':
        vC = np.vectorize(C)
        vS = np.vectorize(S)
    
        z = np.linspace(-50.0, 500.0, 100000.0)
        y = vC(z)
        y2 = vS(z)
    
        fig = pylab.figure()
        ax = fig.add_subplot(111)
        ax.plot(z, y, 'r')
        ax.plot(z, y2, 'b')
        pylab.legend(('$C(z)$', '$S(z)$'), loc = 0)
        pylab.xlim((-50, 0))
        pylab.ylim((0, 12))
        pylab.xlabel('$z$')
        pylab.gca().xaxis.set_major_locator(MaxNLocator(prune = 'lower'))
        pylab.savefig('stumpffneg50to0.eps', format = 'eps')
    
    
        fig2 = pylab.figure()
        ax2 = fig2.add_subplot(111)
        ax2.plot(z, y, 'r')
        ax2.plot(z, y2, 'b')
        pylab.legend(('$C(z)$', '$S(z)$'), loc = 1)
        pylab.xlim((0, 30))
        pylab.ylim((0, 0.5))
        pylab.xlabel('$z$')
        pylab.gca().xaxis.set_major_locator(MaxNLocator(prune = 'lower'))
        pylab.savefig('stumpff0to30.eps', format = 'eps')
    
    
        fig3 = pylab.figure()
        ax3 = fig3.add_subplot(111)
        ax3.plot(z, y, 'r')
        ax3.plot(z, y2, 'b')
        pylab.legend(('$C(z)$', '$S(z)$'), loc = 0)
        pylab.xlim((0, 500))
        pylab.ylim((0, 0.05))
        pylab.xlabel('$z$')
        pylab.gca().xaxis.set_major_locator(MaxNLocator(prune = 'lower'))
        pylab.savefig('stumpff0to500.eps', format = 'eps')
        pylab.show()
    

    然后,你可以使用import stumpff,你可以使用stumpff.C(z)stumpff.S(z)。如果您希望能够在没有stumpff 之前使用它们,请使用from stumpff import *from stumpff import C, S

    【讨论】:

    • 谢谢,我会试一试,如果可行的话,我会按照可接受的方式工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多