python command_Python从命令行直接调用函数-Python run function from the command line | 学步园...

本文讲述了如何在Python工具中遇到API调用错误,需要在板载系统而非服务器执行。作者介绍了在板载Ubuntu系统上安装Python包、使用-c参数执行Python命令以及确保命令传递的raw指令的方法。解决过程涉及模块导入和函数调用,提供了一个实用的PythonCall工具。
摘要由CSDN通过智能技术生成

由来

由于缺乏某python程序直接外部的API调用的支持,于是打算直接使用该python工具内部的api,通过几步设置好环境以后,该API可以调用并且成功。但是,发现调用应用的目标是错误的,在服务器端调用该api不能达到处理的目的,这个api应该在目标板端执行,也就是通过板子命令行执行。

解决之道

1. 板子缺乏相应的python支持,但好在是Ubuntu的系统,安装该python工具需要的包后,将该工具包源码下载,并安装到板子系统中,取得了这个包的API支持。

2. Python的-c参数可以直接在命令行中执行一个python命令

$ python-c'import foo; print foo.hello()'

3. 注意服务器送入板子端的指令应该是raw指令,即不会自动进行变量代换比如"必须写为\"

cmd = ("python -c 'import linaro_image_tools.media_create.boards"

" as lmc_boards; board_type = \"%s\"; rootfs_uuid = \"\";"

" boot_partition = \"/dev/disk/by-label/testboot\";"

" boot_disk = \"/mnt/boot\"; chroot_dir = \"/mnt/root\";"

" boot_device_or_file = \"/dev/null\"; is_live = False;"

" is_lowmem = False; consoles = \"\";"

" board = lmc_boards.board_configs[board_type]();"

" board.set_metadata([\"/mnt/root/tmp/%s\"]);"

" board.populate_boot(chroot_dir, rootfs_uuid, boot_partition, "

"boot_disk, boot_device_or_file, is_live, is_lowmem, consoles)'"

% (board_type, hwpack_name))

参考文档

http://stackoverflow.com/questions/3987041/python-run-function-from-the-command-line

延伸阅读

该插件要求在每一个module下都加上一段语句,该module中的函数才能被调用,见Usage

"""

PythonCall.py

PythonCall is a shortcut that allows a CLI command with arguments to call

any function within a Python module with only two lines of plumbing code.

Usage

=====

* Add code below to bottom of Python module.

if __name__ == "__main__":

import sys, PythonCall

PythonCall.PythonCall(sys.argv).execute()

* From command line or desktop shortcut, build a command of form:

Example: C:\Python25\python.exe C:\Dev\PyUtils\src\TextUtils.py xclip wrap 64

Notes

=====

* Called functions must expect args to be strings and do their own conversions.

* No argument checking or error-checking.

* In case of exception, PythonCall sends message to stderr.

* I often use this with text I pass in via the clipboard, which requires

code to read and write the clipboard (not included here).

Tested with Windows; should work on other platforms.

Jack Trainor 2008

"""

import os, os.path

import sys

import types

class PythonCall(object):

def __init__(self, sysArgs):

try:

self.function = None

self.args = []

self.modulePath = sysArgs[0]

self.moduleDir, tail = os.path.split(self.modulePath)

self.moduleName, ext = os.path.splitext(tail)

__import__(self.moduleName)

self.module = sys.modules[self.moduleName]

if len(sysArgs) > 1:

self.functionName = sysArgs[1]

self.function = self.module.__dict__[self.functionName]

self.args = sysArgs[2:]

except Exception, e:

sys.stderr.write("%s %s\n" % ("PythonCall#__init__", e))

def execute(self):

try:

if self.function:

self.function(*self.args)

except Exception, e:

sys.stderr.write("%s %s\n" % ("PythonCall#execute", e))

#####################################################

# Test Examples - REMOVE

#

# Normally the function calls are in a module other than PythonCall.

# These are only examples.

#

# Example 1: C:\Python25\python.exe C:\Dev\PyUtils\src\PythonCall.py double 14

# Example 2: C:\Python25\python.exe C:\Dev\PyUtils\src\PythonCall.py capitalize "Four score and seven years ago..."

#####################################################

def double(x):

x = int(x)

print 2 * x

def capitalize(s):

print s.upper()

if __name__ == "__main__":

import sys, PythonCall

PythonCall.PythonCall(sys.argv).execute()

#####################################################

# End - REMOVE

#####################################################

-EOF

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值