我正在分发一个具有这种结构的包:
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')
但我想通过它的绝对路径调用脚本,而不是依赖于平台特定的 bin 目录在 上PATH
,如:
# 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))
如何才能做到这一点?谢谢。
编辑删除脚本之外的代码对我来说不是一个实用的解决方案。原因是我将作业分配到集群系统,而我通常这样做的方式是这样的:假设您有一组要运行的任务。我有一个脚本,它将所有任务作为输入,然后调用另一个脚本,该脚本仅在给定任务上运行。就像是:
main.py:
for task in tasks:
cmd = "python script.py %s" %(task)
execute_on_system(cmd)
所以main.py
需要知道在哪里script.py
,因为它需要是一个可执行的命令execute_on_system
。