猿问

Python:调用另一个python脚本

是否可以调用另一个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我如何调用外部python程序,我看到我已经添加了


import stumpff

在那之后,我的新的脚本理解C(z)和S(z)?


慕沐林林
浏览 229回答 1
1回答

不负相思意

脚本的编写方式,无法导入,也无法绘制。为了使其import stumpff能够正常工作,并且您的脚本可以理解C(z)和S(z),您需要制作绘图代码,使其仅在作为脚本运行时才运行。一种方法是将所有内容放入main()函数,然后使用if __name__ == '__main__':&nbsp; &nbsp; main()另外,只需将所有内容置于该条件下即可,如下所示:#!/usr/bin/env ipython#&nbsp; This program plots the Stumpff functions C(z) and S(z)import numpy as npimport pylabfrom matplotlib.ticker import MaxNLocatordef C(z):&nbsp; &nbsp; if z > 0:&nbsp; &nbsp; &nbsp; &nbsp; return (1 - np.cos(z ** 0.5)) / z&nbsp; &nbsp; elif z < 0:&nbsp; &nbsp; &nbsp; &nbsp; return (np.cosh(np.sqrt(-z)) - 1) / -z&nbsp; &nbsp; return 0.5def S(z):&nbsp; &nbsp; if z > 0:&nbsp; &nbsp; &nbsp; &nbsp; return (np.sqrt(z) - np.sin(z ** 0.5)) / np.sqrt(z) ** 3&nbsp; &nbsp; elif z < 0:&nbsp; &nbsp; &nbsp; &nbsp; return (np.sinh(np.sqrt(-z)) - np.sqrt(-z)) / np.sqrt(-z) ** 3&nbsp; &nbsp; return 1.0 / 6.0if __name__ == '__main__':&nbsp; &nbsp; vC = np.vectorize(C)&nbsp; &nbsp; vS = np.vectorize(S)&nbsp; &nbsp; z = np.linspace(-50.0, 500.0, 100000.0)&nbsp; &nbsp; y = vC(z)&nbsp; &nbsp; y2 = vS(z)&nbsp; &nbsp; fig = pylab.figure()&nbsp; &nbsp; ax = fig.add_subplot(111)&nbsp; &nbsp; ax.plot(z, y, 'r')&nbsp; &nbsp; ax.plot(z, y2, 'b')&nbsp; &nbsp; pylab.legend(('$C(z)$', '$S(z)$'), loc = 0)&nbsp; &nbsp; pylab.xlim((-50, 0))&nbsp; &nbsp; pylab.ylim((0, 12))&nbsp; &nbsp; pylab.xlabel('$z$')&nbsp; &nbsp; pylab.gca().xaxis.set_major_locator(MaxNLocator(prune = 'lower'))&nbsp; &nbsp; pylab.savefig('stumpffneg50to0.eps', format = 'eps')&nbsp; &nbsp; fig2 = pylab.figure()&nbsp; &nbsp; ax2 = fig2.add_subplot(111)&nbsp; &nbsp; ax2.plot(z, y, 'r')&nbsp; &nbsp; ax2.plot(z, y2, 'b')&nbsp; &nbsp; pylab.legend(('$C(z)$', '$S(z)$'), loc = 1)&nbsp; &nbsp; pylab.xlim((0, 30))&nbsp; &nbsp; pylab.ylim((0, 0.5))&nbsp; &nbsp; pylab.xlabel('$z$')&nbsp; &nbsp; pylab.gca().xaxis.set_major_locator(MaxNLocator(prune = 'lower'))&nbsp; &nbsp; pylab.savefig('stumpff0to30.eps', format = 'eps')&nbsp; &nbsp; fig3 = pylab.figure()&nbsp; &nbsp; ax3 = fig3.add_subplot(111)&nbsp; &nbsp; ax3.plot(z, y, 'r')&nbsp; &nbsp; ax3.plot(z, y2, 'b')&nbsp; &nbsp; pylab.legend(('$C(z)$', '$S(z)$'), loc = 0)&nbsp; &nbsp; pylab.xlim((0, 500))&nbsp; &nbsp; pylab.ylim((0, 0.05))&nbsp; &nbsp; pylab.xlabel('$z$')&nbsp; &nbsp; pylab.gca().xaxis.set_major_locator(MaxNLocator(prune = 'lower'))&nbsp; &nbsp; pylab.savefig('stumpff0to500.eps', format = 'eps')&nbsp; &nbsp; pylab.show()然后,您可以使用import stumpff,并且可以使用stumpff.C(z)和stumpff.S(z)。如果您想不使用它们就可以使用stumpff它们,请使用from stumpff import *,或from stumpff import C, S
随时随地看视频慕课网APP

相关分类

Python
我要回答