0

File structure:

  • MYTOOL.sh
  • main.py
  • setup.py

My goal is to get MYTOOL.sh running when a user types: $ MYTOOL.

In my setup.py file I have the following:

class PostInstallCommand(install):
    def run(self):
        subprocess.call("echo source " + getSetuptoolsScriptDir() + "/MYTOOL.sh > /usr/local/bin/MYTOOL && chmod +x /usr/local/bin/MYTOOL", shell=True)
        install.run(self)

def getSetuptoolsScriptDir():
    dist = Distribution({'cmdclass': {'install': OnlyGetScriptPath}})
    dist.dry_run = True
    dist.parse_config_files()
    command = dist.get_command_obj('install')
    command.ensure_finalized()
    command.run()
    return dist.install_scripts

setuptools.setup(
    # ... other settings ...
    scripts=['MYTOOL.sh'],
    cmdclass={
        'install': PostInstallCommand,
    }
)

MYTOOL.sh:

#!/bin/bash
PYTHON_VERSION=$(python -c 'import sys; print(".".join(map(str, sys.version_info[0:1])))')
if [ $PYTHON_VERSION -eq 3 ]
then
    python main.py "$@"
else
    python3 main.py "$@"
fi

PS:
I did find this question but it seems unanswered.

Bob van Luijt
  • 7,153
  • 12
  • 58
  • 101
  • What is the point of the shell script? You can just call the Python script itself. – alkasm May 21 '19 at 16:57
  • My script only runs with python3. So I want to make sure it is python3. – Bob van Luijt May 21 '19 at 16:58
  • You can do that without a shell script..just tell your python script which python to use with the shebang. – alkasm May 21 '19 at 16:58
  • Aha, you have a link or example, please? – Bob van Luijt May 21 '19 at 16:59
  • 1
    Just like your shebang in the bash script has `#!/path/to/bash` you can put a similar shebang for Python `#!/path/to/python3` at the top of your `main.py`, then if you change permissions so you can run it like you do with your shell script, then you can just `main arg1 arg2 ...` and it will run. Works just the same as a bash script. – alkasm May 21 '19 at 17:01

0 Answers0