I am distributing a package that has this structure:
mymodule:
mymodule/__init__.py
mymodule/code.py
scripts/script1.py
scripts/script2.py
The mymodule
subdir of mymodule
contains code, and the scripts
subdir contains scripts that should be executable by the user.
When describing a package installation in setup.py
, I use:
scripts=['myscripts/script1.py']
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')
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))
How can this be done? thanks.
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)
so main.py
needs to know where script.py
is, because it needs to be a command executable by execute_on_system
.
execute_on_system()
look like? – Slavicexecute_on_system
takes as input a string corresponding to a shell command. It then creates a temporary file calledcmd.sh
and puts in it the command, along with necessary prefixes that state how the job should run (specific to the cluster system being used on the user's system) -- eg what queue the job should go in, etc. Then it takes this script,cmd.sh
and executes it in a system specific way, e.g. by doing:os.system("qsub cmd.sh")
– Adjutantcmd.sh
shell script. If I understand above you transfercmd.sh
script to the target machine, where the task will be run. Can't you transferscript.py
the same way so that you know the location of this script on the target machine? – Slavic