【发布时间】:2012-10-10 03:27:42
【问题描述】:
我正在分发具有这种结构的包:
mymodule:
mymodule/__init__.py
mymodule/code.py
scripts/script1.py
scripts/script2.py
mymodule 的 mymodule 子目录包含代码,scripts 子目录包含用户可执行的脚本。
在setup.py 中描述包安装时,我使用:
scripts=['myscripts/script1.py']
指定脚本应该去哪里。在安装过程中,它们通常进入某个平台/用户特定的bin 目录。我在mymodule/mymodule 中的代码需要调用脚本。然后找到这些脚本的完整路径的正确方法是什么?理想情况下,它们应该在用户的路径上,所以如果我想从 shell 中调用它们,我应该能够做到:
os.system('script1.py args')
但我想通过其绝对路径调用脚本,而不是依赖于 PATH 上的平台特定 bin 目录,如下所示:
# get the directory where the scripts reside in current installation
scripts_dir = get_scripts_dir()
script1_path = os.path.join(scripts_dir, "script1.py")
os.system("%s args" %(script1_path))
如何做到这一点?谢谢。
EDIT 删除脚本之外的代码对我来说不是一个实用的解决方案。原因是我将作业分发到集群系统,而我通常这样做的方式是这样的:假设您有一组要运行的任务。我有一个脚本,它将所有任务作为输入,然后调用另一个脚本,该脚本仅在给定任务上运行。比如:
main.py:
for task in tasks:
cmd = "python script.py %s" %(task)
execute_on_system(cmd)
所以main.py 需要知道script.py 在哪里,因为它需要是execute_on_system 可执行的命令。
【问题讨论】:
-
execute_on_system()长什么样子? -
@PiotrDobrogost:
execute_on_system将对应于 shell 命令的字符串作为输入。然后它会创建一个名为cmd.sh的临时文件,并在其中放入命令,以及说明作业应该如何运行的必要前缀(特定于用户系统上使用的集群系统)——例如,作业应该进入哪个队列in 等。然后它接受这个脚本cmd.sh并以系统特定的方式执行它,例如通过这样做:os.system("qsub cmd.sh") -
看来问题源于您坚持从
cmd.shshell 脚本运行 Python 脚本。如果我在上面理解你将cmd.sh脚本传输到目标机器,任务将在其中运行。不能用同样的方式转移script.py,让你知道这个脚本在目标机器上的位置吗?
标签: python setuptools distutils setup.py distribute