为了账号安全,请及时绑定邮箱和手机立即绑定

Python:调用另一个python脚本

Python:调用另一个python脚本

慕沐林林 2021-03-18 23:06:58
是否可以调用另一个python脚本来访问脚本中的定义,而不能访问其他内容?在我要导入的脚本中,有一些图要抑制,因为该其他程序不需要。也就是说,我只想访问Stumpff函数的定义而无需绘制图形。我要导入的脚本是:#!/usr/bin/env ipython#  This program plots the Stumpff functions C(z) and S(z)import numpy as npimport pylabfrom matplotlib.ticker import MaxNLocatordef 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.5def 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.0vC = 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我如何调用外部python程序,我看到我已经添加了import stumpff在那之后,我的新的脚本理解C(z)和S(z)?
查看完整描述

1 回答

?
不负相思意

TA贡献1777条经验 获得超10个赞

脚本的编写方式,无法导入,也无法绘制。


为了使其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


查看完整回答
反对 回复 2021-03-30
  • 1 回答
  • 0 关注
  • 228 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号