简体   繁体   English

Python:调用另一个Python脚本

[英]Python: calling another python script

Is it possible to call another python script to access just the definitions inside the script and nothing else? 是否可以调用另一个python脚本来访问脚本中的定义,而不能访问其他内容?

In the script I want to import, there are plots that I want to suppress since there an not needed for this other program. 在我要导入的脚本中,有一些图要抑制,因为该其他程序不需要。 That is, I only want to access the definitions of the Stumpff functions without plotting the figures. 也就是说,我只想访问Stumpff函数的定义而无需绘制图形。

The script I want to import is: 我要导入的脚本是:

#!/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()

From reading python how do I call external python programs , I see that I have add 通过阅读python我如何调用外部python程序 ,我看到我已经添加了

import stumpff

After that, will my new script understand C(z) and S(z) ? 之后,我的新脚本会理解C(z)S(z)吗?

The way your script is written, there is no way to import it and not have the plots made. 脚本的编写方式,无法导入,也无法绘制。

To make it so that import stumpff will work, and your script will understand C(z) and S(z), you'll need to make the plotting code such that it will only run if you are running as a script. 为了使其能够正常工作,并且import stumpff可以工作,并且您的脚本可以理解C(z)和S(z),您需要制作绘图代码,使其仅在作为脚本运行时才运行。 One way to do this is to put all of it in a main() function, and then use 一种方法是将所有内容放入main()函数,然后使用

if __name__ == '__main__':
    main()

Alternatively, simply have all of it underneath that condition, like this: 另外,只需将所有内容置于该条件下即可,如下所示:

#!/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()

Then, you can use import stumpff , and you can use stumpff.C(z) and stumpff.S(z) . 然后,您可以使用import stumpff ,也可以使用stumpff.C(z)stumpff.S(z) If you want to be able to use them without stumpff before them, then use from stumpff import * , or from stumpff import C, S 如果您希望能够在没有stumpff情况下使用它们,则可以from stumpff import *from stumpff import C, S

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM