我正在分发一个具有以下结构的包:
mymodule:
mymodule/__init__.py
mymodule/code.py
scripts/script1.py
scripts/script2.py
The mymodule
的子目录mymodule
包含代码,并且scripts
subdir 包含用户可执行的脚本。
在描述软件包安装时setup.py
, 我用:
scripts=['myscripts/script1.py']
指定脚本应该放在哪里。在安装过程中,它们通常会进入某些特定于平台/用户的状态bin
目录。我的代码mymodule/mymodule
但需要调用脚本。找到这些脚本的完整路径的正确方法是什么?理想情况下,它们此时应该位于用户的路径上,因此如果我想从 shell 中调用它们,我应该能够执行以下操作:
os.system('script1.py args')
但我想通过绝对路径调用脚本,而不是依赖于平台特定的 bin 目录PATH
, as in:
# 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)
so main.py
需要知道在哪里script.py
是,因为它需要是一个可执行命令execute_on_system
.