在distutils中从setup.py查找脚本目录的正确方法? [英] correct way to find scripts directory from setup.py in Python distutils?

查看:127
本文介绍了在distutils中从setup.py查找脚本目录的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在分发具有以下结构的软件包:

I am distributing a package that has this structure:

mymodule:
  mymodule/__init__.py
  mymodule/code.py
  scripts/script1.py
  scripts/script2.py

mymodulemymodule子目录包含代码,而scripts子目录包含用户应可执行的脚本.

The mymodule subdir of mymodule contains code, and the scripts subdir contains scripts that should be executable by the user.

setup.py中描述软件包安装时,我使用:

When describing a package installation in setup.py, I use:

scripts=['myscripts/script1.py']

指定脚本的位置.在安装过程中,它们通常位于特定于平台/用户的bin目录中.我在mymodule/mymodule中拥有的代码需要调用脚本.然后找到这些脚本的完整路径的正确方法是什么?理想情况下,此时它们应该位于用户的路径上,因此,如果我想从外壳中调用它们,则应该能够做到:

To specify where scripts should go. During installation they typically go in some platform/user specific bin directory. The code that I have in mymodule/mymodule needs to make calls to the scripts though. What is the correct way to then find the full path to these scripts? Ideally they should be on the user's path at that point, so if I want to call them out from the shell, I should be able to do:

os.system('script1.py args')

但是我想通过脚本的绝对路径来调用脚本,而不是依赖于平台特定的bin目录位于PATH上,如:

But I want to call the script by its absolute path, and not rely on the platform specific bin directory being on the 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 removing the code outside of a script is not a practical solution for me. the reason is that I distribute jobs to a cluster system and the way I usually do it is like this: imagine you have a set of tasks you want to run on. I have a script that takes all tasks as input and then calls another script, which runs only on the given task. Something like:

main.py:
for task in tasks:
  cmd = "python script.py %s" %(task)
  execute_on_system(cmd)

所以main.py需要知道script.py在哪里,因为它必须是execute_on_system可执行的命令.

so main.py needs to know where script.py is, because it needs to be a command executable by execute_on_system.

推荐答案

我认为您应该对代码进行结构化,以便无需从代码中调用脚本.将所需的代码从脚本移动到包中,然后就可以从脚本和代码中调用此代码.

I think you should structure your code so that you don't need to call scripts from you code. Move code you need from scripts to your package and then you can call this code both from your scripts and from your code.

这篇关于在distutils中从setup.py查找脚本目录的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆